-9

Let's say I have this code:

foo = int(input("Number"))
bar = int(input("Number"))
for number in range(0, 10):
    if foo*bar > 0:
        print("hello")

But, I could also have this code:

foo = int(input("Number"))
bar = int(input("Number"))
top = foo*bar
for number in range(0, 10):
    if top > 0:
        print("hello")

Which one is faster?

Using Python 3.


I realize that this question is similar, however, it's for Java, and Python may be different. Additonally, they're asking about memory efficiency whereas I am asking about processor efficiency.

Quelklef
  • 1,999
  • 2
  • 22
  • 37
  • 3
    I fail to see where you're doing division multiple times there... – TigerhawkT3 Aug 22 '15 at 21:21
  • Oh, you entirely changed the question, gotcha. – TigerhawkT3 Aug 22 '15 at 21:21
  • 2
    Why don't you `timeit` and find out? – jonrsharpe Aug 22 '15 at 21:22
  • did you try running some tests? – Padraic Cunningham Aug 22 '15 at 21:22
  • @TigerhawkT3 yeah, I had realized I didn't ask what I meant to – Quelklef Aug 22 '15 at 21:22
  • @jonrsharpe Would this really be accurate in a small amount of time when I have multiple other programs open, affecting performence? – Quelklef Aug 22 '15 at 21:23
  • Why don't you read the docs and find out what its limitations are? For such a contrived example this exercise seems pointless anyway. – jonrsharpe Aug 22 '15 at 21:24
  • @jonrsharpe I'm a beginner and docs tend to scare me. I'll take a look around, but I'm not really sure what to search for... How variables are stored, i.e. will top `= 12` if foo is 6 and bar is 2, or will top always just `= foo*bar`, meaning it does it every time anyway? – Quelklef Aug 22 '15 at 21:25
  • Well you should get over that or give up, frankly. Python's documentation is pretty good, and accessibly written. And no, Python will store the *result*, not the *computation* - the identifier `top` will reference the integer object `12`. – jonrsharpe Aug 22 '15 at 21:26
  • @jonrsharpe So, then, that means it *will* be faster? – Quelklef Aug 22 '15 at 21:27
  • Go satisfy your own curiosity! Or just think about it - the logical answer is usually the correct one. – jonrsharpe Aug 22 '15 at 21:27
  • 3
    Using Python 3 this leads to an `TypeError`, you cannot multiply strings. Aside of this, on slow displays, first will be faster, because it prints one Hello less. – Daniel Aug 22 '15 at 21:30
  • @Daniel While you are correct, that's not really what this question is about -- the code is used is just for example. – Quelklef Aug 22 '15 at 21:31
  • Quelklef have you actually tested your code whatsoever? It seems really odd... – Thomas Wagenaar Aug 22 '15 at 21:33
  • @ThomasWagenaar Again, the code above is just example code. What I'm actually using is different. – Quelklef Aug 22 '15 at 21:36
  • Okey, but for this piece of code, you should switch the for and if around; why would you check the same statement 10 times in a row? Why not just once and if it is true print hello x10? – Thomas Wagenaar Aug 22 '15 at 21:38
  • @ThomasWagenaar Did you read my question? – Quelklef Aug 22 '15 at 22:16
  • 1
    So why don't you show code, that is correct and more realistic? – Daniel Aug 22 '15 at 22:32
  • @Daniel doesn't make a difference... This code shows precisely what I want. – Quelklef Aug 22 '15 at 22:33
  • The `print("hello")` inside the loop makes the question moot, like trying to paddle an ocean liner with one paddle or two. – Mike Dunlavey Aug 22 '15 at 22:45
  • @MikeDunlavey What do you mean? – Quelklef Aug 22 '15 at 22:51
  • I'm surprised that no one has brought up this: the title says "division" but there's no division in this code. Should the title say "multiplication" or should the code be modified to use `/` instead of `*`... Or should this just be closed/deleted as entirely pointless anyways? – ArtOfWarfare Aug 22 '15 at 23:58
  • 3
    I see this all the time. Somebody asks something like is ++i faster than i++ which if it makes a difference at all is like a nanosecond, and they do it in a loop with a `print` statement that make milliseconds. So the `print` takes 99.99999% of the time, but they don't think of that. They're focusing on the gnat's eyelash. – Mike Dunlavey Aug 23 '15 at 00:04
  • @MikeDunlavey Except that, agian, it's just an example. I have not timed this, though, if I do, I will keep that in mind. – Quelklef Aug 23 '15 at 02:44
  • @ArtOfWarfare You're absolutely right -- my question stemmed from code that used division, hence the title; when I created the sample code, I just used multiplication. – Quelklef Aug 23 '15 at 02:45
  • If this code shows, what you want, then move the `if` outside the loop and your done. – Daniel Aug 23 '15 at 06:44

1 Answers1

1

Just so we can end this already, here is the answer. Using timeit on my computer we get the results as...

Do the multiplication in the loop every time:

$ python -m timeit 'for _ in xrange(10): 10 * 20 > 0'
1000000 loops, best of 3: 1.3 usec per loop

Do the multiplication outside the loop:

$ python -m timeit 'foo = 10 * 20
> for _ in xrange(10): foo > 0'
1000000 loops, best of 3: 1.07 usec per loop

Running the multiplication outside the loop instead of inside of it saves us 0.23 microseconds - about 18%.

Note that I'm using Python 2.7.10 on a 2007 iMac (2.4 GHz) running OS X 10.11.0, developer preview 7. Your exact results may vary with Python version, hardware, OS Version, CPU... other applications you have running (just Safari with 3 tabs in my case). But you should comparably see that storing the number in a variable is trivial compared to repeatedly performing the same operation and discarding the results every time.

In the future, OP, please just use timeit. It comes built in with Python for doing performance checks just like this. See the documentation here: https://docs.python.org/3/library/timeit.html (On that note, do not fear the documentation. Read them. If you have questions, ask them on Stack Overflow - you may reveal short-comings in the documentation where they need to be improved/clarified. Or at the very least, your questions will be answered and you'll know more afterwards than you did before.)

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
  • I really shouldn't have asked in the first place, since the answer is intuitive, but I guess I wanted to know if *maybe* it wasn't. I'm not so scared of docs as I tend to find them hard to read, and it was my mistake to post here first instead of looking around. Timeit seems useful -- I just tested it on this question, and the results seem inconclusive, but that's beside the point at this state of the question. Overall, my rep is hurting from this question, but I learned some. So thank you guys, and thank you for doing the test and finishing this. – Quelklef Aug 23 '15 at 04:16