-2

In real life which is more efficient?

  1. Running a print statement 100 times
  2. Running a print statement 10000 times

The computer science complexity says it would be big O(n) so they should be equal. But does this differ in real life?

I'd think that 100 times is more efficient.

A. Sarid
  • 3,916
  • 2
  • 31
  • 56
Yahoo
  • 4,093
  • 17
  • 59
  • 85
  • All other factors being the same, I would actually expect the 10000 prints to be more efficient as compared to the 100. The reason for this is that the 10000 prints have a greater economy of scale. – Tim Biegeleisen Sep 23 '16 at 07:12
  • 1
    When the time complexity of the algorithm is mentioned as O(1) it means that it takes constant time. Please refer to this question: http://stackoverflow.com/questions/697918/what-does-o1-access-time-mean – Aaditya Gavandalkar Sep 23 '16 at 07:13
  • The key word here is efficient. Which one would be faster, that's different. – Sam Orozco Sep 23 '16 at 07:13
  • Your question is lacking rigor, which makes it impossible to answer. In "it would be big O...", what you mean by "it" is unclear. And, worse, you say O(n), but without specifying what n denotes. Nor is there any precise notion of efficiency "in real life". Lastly, is your question about complexity or efficiency ? –  Sep 23 '16 at 13:17
  • Guys i still didn't understand. Out of the two which one would I prefer ? In terms of fastness and complexity. – Yahoo Sep 23 '16 at 15:33

3 Answers3

0

It will not take O(N) because your input size is constant.Time complexity of O(1) means, algorithm will take constant time irrespective of input size. In your case 100 times or 10000 times are constant.

hard coder
  • 5,449
  • 6
  • 36
  • 61
  • If I say that the loop runs for n times and the value of n is 100 then would the complexity change ? – Yahoo Sep 23 '16 at 15:32
0

There is no point in talking about efficiency in such a way.

If your task is to print 100 or 1000 lines, "efficiency" lies not in the number of repetitions.

"Efficiency" would is about doing things in an optimal way; not looking at number of repetitions. In that sense, you would probably worry about flushing caches, IO buffering, and such things when worrying about the efficiency of print statements.

And: what you are saying is that "iterate 1 to n and print lines" has O(n)! So, you dont pick two different n's and say: because the underlying task is O(n); doing it 100 times is the same as doing it 1000 times.

Meaning: Big O is meant to tell you about potential cost of a certain operation; it is not meant to say anything about specific instances of n.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

From wikipedia Big-O:

Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity.

So yes, for an input n your printing algorithm time-complexity is O(n).

When you are talking about efficiency, it really depends on your implementation and hardware etc, but in theory they will actually be equally.

When you are talking about who will be faster, then yes, usually the one with smaller input size will be faster.

A. Sarid
  • 3,916
  • 2
  • 31
  • 56
  • Thanks for the reply...What does "limiting behavior" mean? Can I link it to fastness or efficiency? Why would I care about limiting behavior as a developer? – Yahoo Sep 23 '16 at 15:30
  • 1) I've completed the whole sentence, please also read the page in the given link - it is some kind of asymptotic notation. 2) Comparing different algorithms with different Big-O and comparing different input sizes to the same algorithm is two different things. For a given problem A we usually speak about time-efficiency in terms of Big-O, although, it doesn't necessarily means it will be faster in real life. – A. Sarid Sep 23 '16 at 17:16