Iteration style

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abdullah Khaidar

    #1

    Iteration style

    Is there any iteration style we must use to get faster processing
    time? I've tried with some style to concat number in list. But I still
    don't know which one is the recommended style.
    [color=blue][color=green][color=darkred]
    >>> def useListIteratio n():[/color][/color][/color]
    list = [str(element) for element in range(5)]
    result = ""
    for item in list:
    result += item
    return result
    [color=blue][color=green][color=darkred]
    >>> def useNormalIterat ion():[/color][/color][/color]
    list = [str(element) for element in range(5)]
    result = ""
    for index in range(len(list) ):
    result += list[index]
    return result
    [color=blue][color=green][color=darkred]
    >>> def useJoin():[/color][/color][/color]
    list = [str(element) for element in range(5)]
    return "".join(lis t)
    [color=blue][color=green][color=darkred]
    >>> useListIteratio n()[/color][/color][/color]
    '01234'[color=blue][color=green][color=darkred]
    >>> useNormalIterat ion()[/color][/color][/color]
    '01234'[color=blue][color=green][color=darkred]
    >>> useJoin()[/color][/color][/color]
    '01234'[color=blue][color=green][color=darkred]
    >>> def getTimer():[/color][/color][/color]
    from timeit import Timer
    t1 = Timer("useListI teration", "from __main__ import
    useListIteratio n")
    t2 = Timer("useNorma lIteration", "from __main__ import
    useNormalIterat ion")
    t3 = Timer("useJoin" , "from __main__ import useJoin")
    print "Using list iteration: ", min(t1.repeat() )
    print "Using normal iteration: ", min(t2.repeat() )
    print "Using join: " , min(t3.repeat() )

    [color=blue][color=green][color=darkred]
    >>> getTimer()[/color][/color][/color]
    Using list iteration: 0.0700158819068
    Using normal iteration: 0.0701589168456
    Using join: 0.0685698880724[color=blue][color=green][color=darkred]
    >>> getTimer()[/color][/color][/color]
    Using list iteration: 0.070928567737
    Using normal iteration: 0.0698613929983
    Using join: 0.0693454056312[color=blue][color=green][color=darkred]
    >>> getTimer()[/color][/color][/color]
    Using list iteration: 0.0683511451874
    Using normal iteration: 0.0698585993471
    Using join: 0.0708022947051[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
  • Michael Hoffman

    #2
    Re: Iteration style

    Abdullah Khaidar wrote:
    [color=blue]
    > Is there any iteration style we must use to get faster processing
    > time?[/color]

    Yes, definitely this one:
    [color=blue][color=green][color=darkred]
    >>>>def useJoin():[/color][/color]
    >
    > list = [str(element) for element in range(5)]
    > return "".join(lis t)[/color]

    You aren't going to get the right results from your getTimer() function
    because you are just timing putting the function object on the stack,
    rather than actually calling it.
    [color=blue][color=green][color=darkred]
    >>>>def getTimer():[/color][/color]
    >
    > from timeit import Timer
    > t1 = Timer("useListI teration", "from __main__ import
    > useListIteratio n")[/color]

    should be t1 = Timer("useListI teration()", "from __main__ import
    useListIteratio n")
    [color=blue]
    > print "Using list iteration: ", min(t1.repeat() )[/color]

    Of course now that you are actually calling a function, it should take a
    lot longer, so repeating it 3 * 1,000,000 times is way too many. I would
    change the number of integers you are concatenating from 5 to 1000 and
    run Timer.timeit(nu mber=100) (repeats 3 * 100 times) instead.
    [color=blue][color=green][color=darkred]
    >>> getTimer()[/color][/color][/color]
    Using list iteration: 0.751999855042
    Using normal iteration: 0.766999959946
    Using join: 0.446000099182

    HTH,
    --
    Michael Hoffman

    Comment

    • Abdullah Khaidar

      #3
      Re: Iteration style

      Thanks for your correction. Now I've found that using join (list
      methods) is better than others.

      --M.Abdullah Khaidar



      Michael Hoffman <m.h.3.9.1.with out.dots.at.cam .ac.uk@example. com> wrote in message news:<ciubec$ob q$1@pegasus.csx .cam.ac.uk>...[color=blue]
      > Abdullah Khaidar wrote:
      >[color=green]
      > > Is there any iteration style we must use to get faster processing
      > > time?[/color]
      >
      > Yes, definitely this one:
      >[color=green][color=darkred]
      > >>>>def useJoin():[/color]
      > >
      > > list = [str(element) for element in range(5)]
      > > return "".join(lis t)[/color]
      >
      > You aren't going to get the right results from your getTimer() function
      > because you are just timing putting the function object on the stack,
      > rather than actually calling it.
      >[color=green][color=darkred]
      > >>>>def getTimer():[/color]
      > >
      > > from timeit import Timer
      > > t1 = Timer("useListI teration", "from __main__ import
      > > useListIteratio n")[/color]
      >
      > should be t1 = Timer("useListI teration()", "from __main__ import
      > useListIteratio n")
      >[color=green]
      > > print "Using list iteration: ", min(t1.repeat() )[/color]
      >
      > Of course now that you are actually calling a function, it should take a
      > lot longer, so repeating it 3 * 1,000,000 times is way too many. I would
      > change the number of integers you are concatenating from 5 to 1000 and
      > run Timer.timeit(nu mber=100) (repeats 3 * 100 times) instead.
      >[color=green][color=darkred]
      > >>> getTimer()[/color][/color]
      > Using list iteration: 0.751999855042
      > Using normal iteration: 0.766999959946
      > Using join: 0.446000099182
      >
      > HTH,[/color]

      Comment

      Working...