The International Simutrans Forum

Development => Patches & Projects => Incorporated Patches and Solved Bug Reports => Topic started by: kierongreen on April 15, 2012, 05:01:08 AM

Title: replacing FOR with for in new month
Post by: kierongreen on April 15, 2012, 05:01:08 AM
This barely seems worth it in the overall scheme f things, however in karte_t::neuer_monat() we have:
    FOR(weighted_vector_tpl<stadt_t*>, const i, stadt) {
         stadt.update(i, i->get_einwohner());
     }


each stadt.update loops over stadt updating the i th value (a loop within a loop)

    for(uint16 i=0; i<stadt.get_count(); i++) {
        stadt.update_at(i, stadt[i]->get_einwohner());
    }


should do the same but avoiding a double loop. On my computer this takes half the time of FOR, unfortunately this only cuts the time taken from 4ms to 2ms, once a month, and that is with 1000 cities...

Still, every little helps...
Title: Re: replacing FOR with for in new month
Post by: Tron on April 15, 2012, 03:02:05 PM
Currently this piece of code is O(n^3) in the number of cities. With your suggestion it is O(n^2) (update_at() again has a loop over the elements of the vector). I changed it, so it takes just O(n), i.e. one pass over all cities.