News:

Simutrans Tools
Know our tools that can help you to create add-ons, install and customize Simutrans.

replacing FOR with for in new month

Started by kierongreen, April 15, 2012, 05:01:08 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kierongreen

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...

Tron

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.