473,789 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 useListIteratio n(): list = [str(element) for element in range(5)]
result = ""
for item in list:
result += item
return result
def useNormalIterat ion(): 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(lis t)
useListIteratio n() '01234' useNormalIterat ion() '01234' useJoin() '01234' def getTimer(): 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() )

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 2082
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(lis t)


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("useListI teration", "from __main__ import
useListIteratio n")


should be t1 = Timer("useListI teration()", "from __main__ import
useListIteratio n")
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(nu mber=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(lis t)


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("useListI teration", "from __main__ import
useListIteratio n")


should be t1 = Timer("useListI teration()", "from __main__ import
useListIteratio n")
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(nu mber=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
3752
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 ------------------------------------------------------------- PEP: 323
59
4347
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 HTML version is much more readable than the ReST version. See: http://www.python.org/peps/pep-0322.html
4
2943
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 simplified example. The goal is to parse and build an AST for a print statement, returning the base-node of the AST built. print ::= "print" expr | string ("," expr | string )*
1
425
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. { .... system("cls"); printf("\nIteration:%d", i);
23
22011
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
5644
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 complexity as I go. Here's a simple one I tried out: #include<stdio.h> /* To compare the the time and space cost of iteration against
0
1629
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
2071
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 being passed, document.getElementById(), loses value, so the populateList function cannot find ANY of the elements. if (tip>1){ document.getElementById("grpbook").style.visibility="visible"; document.form1.unit.style.visibility="visible"; ...
1
3369
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 the iteration variable is used and you cannot modify it. For example: int pins = {9, 3, 7, 2} int newPin = 10; foreach(int pin in pins) {
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6765
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.