473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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.
def useListIteration(): list = [str(element) for element in range(5)]
result = ""
for item in list:
result += item
return result
def useNormalIteration(): list = [str(element) for element in range(5)]
result = ""
for index in range(len(list)):
result += list[index]
return result
def useJoin(): list = [str(element) for element in range(5)]
return "".join(list)
useListIteration() '01234' useNormalIteration() '01234' useJoin() '01234' def getTimer(): from timeit import Timer
t1 = Timer("useListIteration", "from __main__ import
useListIteration")
t2 = Timer("useNormalIteration", "from __main__ import
useNormalIteration")
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())

getTimer() Using list iteration: 0.0700158819068
Using normal iteration: 0.0701589168456
Using join: 0.0685698880724 getTimer() Using list iteration: 0.070928567737
Using normal iteration: 0.0698613929983
Using join: 0.0693454056312 getTimer() Using list iteration: 0.0683511451874
Using normal iteration: 0.0698585993471
Using join: 0.0708022947051

Jul 18 '05 #1
2 2056
Abdullah Khaidar wrote:
Is there any iteration style we must use to get faster processing
time?
Yes, definitely this one:
def useJoin():
list = [str(element) for element in range(5)]
return "".join(list)


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.
def getTimer():
from timeit import Timer
t1 = Timer("useListIteration", "from __main__ import
useListIteration")


should be t1 = Timer("useListIteration()", "from __main__ import
useListIteration")
print "Using list iteration: ", min(t1.repeat())


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(number=100) (repeats 3 * 100 times) instead.
getTimer()

Using list iteration: 0.751999855042
Using normal iteration: 0.766999959946
Using join: 0.446000099182

HTH,
--
Michael Hoffman
Jul 18 '05 #2
Thanks for your correction. Now I've found that using join (list
methods) is better than others.

--M.Abdullah Khaidar
http://khaidarmak.blogspot.com
Michael Hoffman <m.*********************************@example.com > wrote in message news:<ci**********@pegasus.csx.cam.ac.uk>...
Abdullah Khaidar wrote:
Is there any iteration style we must use to get faster processing
time?


Yes, definitely this one:
>def useJoin():


list = [str(element) for element in range(5)]
return "".join(list)


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.
>def getTimer():


from timeit import Timer
t1 = Timer("useListIteration", "from __main__ import
useListIteration")


should be t1 = Timer("useListIteration()", "from __main__ import
useListIteration")
print "Using list iteration: ", min(t1.repeat())


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(number=100) (repeats 3 * 100 times) instead.
>>> getTimer()

Using list iteration: 0.751999855042
Using normal iteration: 0.766999959946
Using join: 0.446000099182

HTH,

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
4
by: Ed Davis | last post by:
I'm trying to decide which of the following programming styles is better, as in easier to understand, and thus easier to maintain. Keep in mind that for posting purposes, this is a greatly...
1
by: karthigan | last post by:
I am writing a simple hardware test program in C that would run from Windows command line. Inside one of the loops, I have this code fragment that would display the Iteration count. { .......
23
by: Mitchell Vincent | last post by:
Is there any way to "skip" iterations in a for loop? Example : for x = 1 to 10 if something = 1 next endif
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
0
by: mkyrou | last post by:
Community > Programming Help > .NET crystal report and iteration mkyrou Junior Member 3 Posts Today 01:09 PM #1 crystal report and iteration
2
by: Vanecelli | last post by:
The below code is calling a function that populates a certain number of listbox es with options. IT loops as it should if there are only 2 instances of k, but anything greater and the second variable...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.