ProgrammingHard
A program reads a number n and then loops for (i = 0; i < n; i++) with n = 10^9 and a constant body. Roughly how long does it take in C for a simple body?
- A. Less than a millisecond
- B. About 10 minutes
- C. About one second or a little more, which is borderline for typical limits
- D. Unbounded, because the compiler cannot optimise loops
Correct answer: C. About one second or a little more, which is borderline for typical limits
A simple loop runs about 10^9 iterations per second on modern hardware, so the time is borderline. A closed-form answer avoids the loop.
More C Programming questions
- Which operation makes a sorted array of n elements support lookup in O(log n) and insertion in O(n)?
- For a char buffer of length 10^6 that is processed once, which loop is correct and fast enough?
- A matrix of 3000 x 3000 cells is read once and each cell is visited a constant number of times. Which complexity bound applies to the…
- A recursive merge sort on 10^6 elements makes how many comparisons, roughly?
- Which function call costs O(n) time per call and can be avoided by caching its result in a loop?
- A recurrence T(n) = 2 T(n/2) + n describes which algorithm, with which complexity?