c++ - Time complexity of enumerator -
enum class color { blue, red, green, purple, first=blue, last=purple }; color operator++( color& x ) { return x = (color)(((int)(x) + 1)); } color operator*(color c) {return c;} color begin(color r) {return color::first;} // end iterator needs return 1 past end! color end(color r) {return color(int(color::last) + 1);} int main() { (const auto& color : color()) std::cout << int(color); //0123 return 0; } i have taken piece of code link.
i asked time complexity of similar piece of code. per understanding, o(n) enumerator element being iterated.
right answer on platforms says o(1) without explanation.
can confirm, o(1) , why?
when preform asymptotic complexity analysis, it's important define input size is. because that's complexity defined in terms of. analysis meaningful in way.
if instance algorithm defined have no input, can argue number of enumerators fixed , last - first. such loop body executed fixed amount of times, , o(1) it.
i can guess "some platform" part may refer ability of compilers optimize. when compiler sees loop preformed 4 times, may choose unroll it, instead of emitting code actual loop.
having said that, optimizations don't affect asymptotic complexity of algorithms. may @ affect coefficient hidden behind big-oh notation. loop o(1) either way, according analysis did above, coefficient smaller after unrolling optimization, since code pertains looping possibly gone.
Comments
Post a Comment