Thread: A question

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11

    A question


    Which one is fastest of these 2? Is there any faster alternatives? How fast is the one over the other and why?

    Code:
    for number in xrange(len(list)):
    	element = list[number]
    	print element
    	# do more stuff here ..
    or:

    Code:
    for element in list:
    	print element
    	# do more stuff here ..

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Hi,
    The second one is faster than the first one...ofcourse the difference wld be visible in a large program!
    If we take the first piece of code, the 3 lines of Python code will be equivalent to some 20 lines of assembly language code. The first one will have more lines of code...as we are making use of the built-in functions...xrange and len. Now these assembly language lines are converted into opcodes... the Instruction Decoder will convert this opcode into signals... so more the number of instructions...more the opcodes and hence more time wld be taken by the processor... because during the first clock cycle the instruction wld be loaded ...in the second clock cycle it wld be decoded and then finally the program counter wld be incremented ... so each function wld add up to the clock cycles and hence more time to process each instruction. The second one is an optimized way of doing it. Maybe someone cld give the fastest version.... I don't think there exists such a thingy.

    Rgds,
    Subha
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    Originally Posted by NewPythoner
    Hi,
    The second one is faster than the first one...ofcourse the difference wld be visible in a large program!
    If we take the first piece of code, the 3 lines of Python code will be equivalent to some 20 lines of assembly language code. The first one will have more lines of code...as we are making use of the built-in functions...xrange and len. Now these assembly language lines are converted into opcodes... the Instruction Decoder will convert this opcode into signals... so more the number of instructions...more the opcodes and hence more time wld be taken by the processor... because during the first clock cycle the instruction wld be loaded ...in the second clock cycle it wld be decoded and then finally the program counter wld be incremented ... so each function wld add up to the clock cycles and hence more time to process each instruction. The second one is an optimized way of doing it. Maybe someone cld give the fastest version.... I don't think there exists such a thingy.

    Rgds,
    Subha

    Thank you for a very nice answer
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The easiest way to test this kind of thing - and the most fun - is to use the timeit module:

    http://www.python.org/doc/2.3.4/lib/module-timeit.html

    Code:
    >>> import timeit
    >>> 
    >>> someList = list('test list')
    >>> someList
    ['t', 'e', 's', 't', ' ', 'l', 'i', 's', 't']
    >>> 
    >>> testBlock1 = """
    ... for number in xrange(len(someList)):
    ...     element = someList[number]
    ...     print element
    ... """
    >>> testBlock2 = """
    ... for element in someList:
    ...     print element
    ... """
    >>> timeit.Timer(testBlock1, 'from __main__ import someList').timeit(1000)
    t
    e
    s
    t
     
    l
    i
    s
    t
    ...
    1.183743953704834
    >>> timeit.Timer(testBlock2, 'from __main__ import someList').timeit(1000)
    t
    e
    s
    t
     
    l
    i
    s
    t
    ...
    1.1404399871826172
    >>>
    In this example the code block (or function etc.) passed to the Timer object is executed 1000 times, before returning how long this took . This can be very useful when you are optimizing a working program and need to choose between one algorithm and another.

    Take care guys,

    Mark.

    BTW nice post New.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    Originally Posted by netytan
    The easiest way to test this kind of thing - and the most fun - is to use the timeit module:

    http://www.python.org/doc/2.3.4/lib/module-timeit.html

    Code:
    >>> import timeit
    >>> 
    >>> someList = list('test list')
    >>> someList
    ['t', 'e', 's', 't', ' ', 'l', 'i', 's', 't']
    >>> 
    >>> testBlock1 = """
    ... for number in xrange(len(someList)):
    ...     element = someList[number]
    ...     print element
    ... """
    >>> testBlock2 = """
    ... for element in someList:
    ...     print element
    ... """
    >>> timeit.Timer(testBlock1, 'from __main__ import someList').timeit(1000)
    t
    e
    s
    t
     
    l
    i
    s
    t
    ...
    1.183743953704834
    >>> timeit.Timer(testBlock2, 'from __main__ import someList').timeit(1000)
    t
    e
    s
    t
     
    l
    i
    s
    t
    ...
    1.1404399871826172
    >>>
    In this example the code block (or function etc.) passed to the Timer object is executed 1000 times, before returning how long this took . This can be very useful when you are optimizing a working program and need to choose between one algorithm and another.

    Take care guys,

    Mark.

    BTW nice post New.


    thanks for another good tips, I will try this
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    In the example that netytan gave most of the time is spent in the print statement, so the difference between the two ways of looping becomes almost insignificant.

    If you replace the "print element" with "pass" on my system the second version is three times faster.

    Code:
    >>> somelist = list('test list')
    >>> testblock1 = '''
    ... for number in xrange(len(somelist)):
    ... 	element = somelist[number]
    ... 	pass
    ... '''
    >>> testblock2 = '''
    ... for element in somelist:
    ... 	pass
    ... '''
    >>> from timeit import Timer
    
    >>> Timer(testblock1, 'from __main__ import somelist').timeit(100000)
    0.39753879333825637
    
    >>> Timer(testblock2, 'from __main__ import somelist').timeit(100000)
    0.1382186842182449
    Dave - The Developers' Coach
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Hi Daq,

    Guess this wld interest you.........

    http://www.python.org/doc/essays/list2str.html

    Its Guido's essay... wld give you some pointers on loop optimization

    ......since function call overhead in Python is bigger than for loop overhead.
    Wow! this was news for me.

    Rgds,
    Subha
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    Beutiful Thanks alot guys

IMN logo majestic logo threadwatch logo seochat tools logo