1

I want to understand why the first example took twice as long than the second one. My intuition says that converting integer chars to int should be atleast as fast as converting them to float, if not faster.

>>> from timeit import timeit
>>> timeit("float(sum(map(int,['5', '8', '3', '6', '3', '9', '5', '0', '1', '0'])))")
6.165183067321777

>>> timeit("sum(map(float, ['5', '8', '3', '6', '3', '9', '5', '0', '1', '0']))")
2.928786039352417

edit: duplicate of Why is float() faster than int()?

even the presence of extra float call in the first example is not the cause because I ran timeit for it as well and it takes 1% time as compared to these operations (for same number)

>>> from timeit import timeit 
>>> timeit("float(sum(map(int,['5', '8', '3', '6', '3', '9', '5', '0', '1', '0']
)))", number=100000)
0.6287150382995605
>>> timeit("sum(map(float, ['5', '8', '3', '6', '3', '9', '5', '0', '1', '0']))"
,number=100000)
0.2976219654083252
>>> timeit("float(1)",number=100000)
0.02610301971435547

I tried dis, but nothing there helped me either:

def a(li):
  return float(sum(map(int,li)))

def b(li):
  return sum(map(float, li))

# >>> dis(a)
#   4           0 LOAD_GLOBAL              0 (float)
#               3 LOAD_GLOBAL              1 (sum)
#               6 LOAD_GLOBAL              2 (map)
#               9 LOAD_GLOBAL              3 (int)
#              12 LOAD_FAST                0 (li)
#              15 CALL_FUNCTION            2
#              18 CALL_FUNCTION            1
#              21 CALL_FUNCTION            1
#              24 RETURN_VALUE        

# >>> dis(b)
# 4           0 LOAD_GLOBAL              0 (sum)
#             3 LOAD_GLOBAL              1 (map)
#             6 LOAD_GLOBAL              2 (float)
#             9 LOAD_FAST                0 (li)
#            12 CALL_FUNCTION            2
#            15 CALL_FUNCTION            1
#            18 RETURN_VALUE        

I was using python 2.7.12 for this test

Community
  • 1
  • 1
Ayush
  • 3,695
  • 1
  • 32
  • 42
  • 2
    Because function calling in Python is expensive as hell? Most likely things would change with bigger input. – Paulo Scardine Aug 17 '16 at 15:23
  • 1
    This can be simplified to just why `map(float, [...])` is faster than `map(int, [...])`. I'm seeing `int` taking 2.9x the time than `float`. – Dogbert Aug 17 '16 at 15:25
  • @PauloScardine then why did `timeit("float(1)",number=100000)` ran so much fast? do you think python was caching result there? – Ayush Aug 17 '16 at 15:25
  • 7
    http://stackoverflow.com/questions/21232733/why-is-float-faster-than-int – Dogbert Aug 17 '16 at 15:27
  • I'm closing this question^ – Ayush Aug 17 '16 at 15:30
  • 1
    I reopened the question because [_Why is float() faster than int()?_](http://stackoverflow.com/questions/21232733/why-is-float-faster-than-int) was never resolved satisfactorily in my opinion (or the OP of that question). – martineau Aug 17 '16 at 16:17
  • @martineau all due respect (and I mean that, I love your work here) should we really allow dupes for every question not answered to our delight? I would prefer someone answering the other question, because it is clearly superior (or at least more to the point). – Paulo Scardine Aug 17 '16 at 17:39
  • 1
    @Paulo: Thanks...I try to do my best. Regarding this question: I agree that the other question is more direct, but its one answer is unsatisfactory and inconclusive—plus, since it's so old, I doubt it'll see much action at this point. Maybe this one will. I also think you might be right about an extra function call possibly being the cause. – martineau Aug 17 '16 at 18:43

0 Answers0