3 ms·
But the code is actually doing a whole bunch of things, let's say 20, with each loop. So it's O(21n) vs. O(20n). 2n implies looping twice or something, not a
by Dylan16807 13y ago
But the code is actually doing a whole bunch of things, let's say 20, with each loop. So it's O(21n) vs. O(20n).
2n implies looping twice or something, not a single extra operation.
(and 20 is probably a gross underestimate if we're comparing console.log to a mere ==)
- goldenkey 13y agoI made an assumption, you're making an assumption too. I'd agree that in any case where it would actually matters, you'd be doing a small amount of ops in the if statement. The complexity change will depend on what you were doing in the loop as compared to the speed of a single if statement. If it were a large amount of ops, then yes, it'd be a more minor difference.
- Dylan16807 13y agoMy point is that "O(2n)" is an extremely specific and misleading way to characterize 'extra conditional per iteration'. It doesn't matter what the actual percentage difference is.
- deleted 13y ago[deleted]
- goldenkey 13y agoI agree, now what is the correct notation so we all can learn? :-)
- Dylan16807 13y agoIt's completely pointless to use such a notation here in lieu of description or benchmarks, but if you insist then use something like O(kn) vs. O((k+1)n)