c++ - modifying values in pointers is very slow? -
i'm working huge amount of data stored in array, , trying optimize amount of time takes access , modify it. i'm using window, c++ , vs2015 (release mode).
i ran tests , don't understand results i'm getting, love optimizing code.
first, let's have following class:
class foo { public: int x; foo() { x = 0; } void inc() { x++; } int x() { return x; } void addx(int &_x) { _x++; } };
i start initializing 10 million pointers instances of class std::vector of same size.
#include <vector> int count = 10000000; std::vector<foo*> fooarr; fooarr.resize(count); (int = 0; < count; i++) { fooarr[i] = new foo(); }
when run following code, , profile amount of time takes complete, takes approximately 350ms (which, purposes, far slow):
for (int = 0; < count; i++) { fooarr[i]->inc(); //increment elements }
to test how long takes increment integer many times, tried:
int x = 0; (int = 0; < count; i++) { x++; }
which returns in <1ms.
i thought maybe number of integers being changed problem, following code still takes 250ms, don't think it's that:
for (int = 0; < count; i++) { fooarr[0]->inc(); //only increment first element }
i thought maybe array index access bottleneck, following code takes <1ms complete:
int x; (int = 0; < count; i++) { x = fooarr[i]->x(); //set x }
i thought maybe compiler doing hidden optimizations on loop last example (since value of x same during each iteration of loop, maybe compiler skips unnecessary iterations?). tried following, , takes 350ms complete:
int x; (int = 0; < count; i++) { fooarr[i]->addx(x); //increment x inside foo function }
so 1 slow again, maybe because i'm incrementing integer pointer again.
i tried following too, , returns in 350ms well:
for (int = 0; < count; i++) { fooarr[i]->x++; }
so stuck here? ~350ms absolute fastest can increment integer, inside of 10million pointers in vector? or missing obvious thing? experimented multithreading (giving each thread different chunk of array increment) , took longer once started using enough threads. maybe due other obvious thing i'm missing, i'd stay away multithreading keep things simple.
i'm open trying containers other vector too, if speeds things up, whatever container end using, need able resize it, remove elements, etc.
i'm new c++ appreciated!
let's cpu point of view.
incrementing integer means have in cpu register , increments it. fastest option.
i'm given address (vector->member) , must copy register, increment, , copy result address. worst: cpu cache filled vector pointers, not vector-member pointers. few hits, cache "refueling".
if manage have members in vector, cpu cache hits more frequent.
Comments
Post a Comment