While learning the basic knowledge of algorithm, I find puzzle about the time complexity calculation and the real time consumption when run the codes.
The demo codes specify the problem.
function calcDemo1(){
var init = 0;
for(var i=0;i<40;i++){
init += 0;
}
return init;
}
function calcDemo2(){
var init = 0;
init += (0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39);
return init;
}
- Does calcDemo1's time complexity is O(1) even if it's a "for loop"?
- In case their time complexity were both O(1), do they take the same amount of time in the worst-case scenario when run the code?
The relative question is here