473,803 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

slowdown on looping

After running a simple loop around 100-200 million times, there's a
speed drop of about a factor of 7. This happens within several
minutes (on a 1.7 Ghz machine), so it's not that hard to see. I'm
using Python 2.3.2 under Windows.

Is anyone familiar with this?

import time
startTime = time.time()
x = 0
while 1:
x += 1
if x % 1000000 == 0:
endTime = time.time()
print "time = ", endTime - startTime, x
startTime = endTime
Trevor
Jul 18 '05 #1
5 3942
Trevor Perrin wrote:
After running a simple loop around 100-200 million times, there's a
speed drop of about a factor of 7. This happens within several
minutes (on a 1.7 Ghz machine), so it's not that hard to see. I'm
using Python 2.3.2 under Windows.

Is anyone familiar with this?

import time
startTime = time.time()
x = 0
while 1:
x += 1
if x % 1000000 == 0:
endTime = time.time()
print "time = ", endTime - startTime, x
startTime = endTime
Trevor


I tried something similar to the loop above, but used a fixed loop size
(for i in range(100000)) and varied the starting value of x. I get a
slowdown of about a factor of 5 if I start it with x > sys.maxint.
Presumably that is because both += and % are now operating with long
integers rather than integers. Long integers have unlimited precision,
which is not usually supported by hardware arithmetic, so it is bound to
be slower.

Did you initially discover the problem with the loop above, or was that
just a simplified test to analyze the effect? If the latter, then
another loop might slow down for an entierely different cause (e.g.
memory use)

David

Jul 18 '05 #2
Trevor Perrin:
After running a simple loop around 100-200 million times, there's a
speed drop of about a factor of 7. This happens within several
minutes (on a 1.7 Ghz machine), so it's not that hard to see.


Are computers really that fast these days? I'm startled.

Here's what I first thought.

Python's integers in your machine are 32 bit twos-complement
signed integers. The largest number that can represent is
2**31-1 == 2,147,483,648 . If you get beyond that, Python
switches to a 'long', which can store as many digits as you
can feed it memory. Because the math is no longer done as
low-level assembly instructions, the code is slower.

However, that limit is 10 times greater than 200 million, so that
isn't the problem. (Could you double check that you're not
off by an order of magnitude?)

I also wondered if your output was in an IDE which was slowing
down as the screen buffer got more and more flow, but you'll
only have 100 terms on the screen.

So I don't know. Next idea anyone?

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #3
On 3 Oct 2003 22:50:42 -0700, tr***@trevp.net (Trevor Perrin) wrote:
After running a simple loop around 100-200 million times, there's a
speed drop of about a factor of 7. This happens within several
minutes (on a 1.7 Ghz machine), so it's not that hard to see. I'm
using Python 2.3.2 under Windows.

Is anyone familiar with this?

import time
startTime = time.time()
x = 0
while 1:
x += 1
if x % 1000000 == 0:
endTime = time.time()
print "time = ", endTime - startTime, x
startTime = endTime

There will be a point where it switches gears from 32-bit int counting to unlimited
(except for memory) bitwidth long integer counting, i.e., when the count becomes 2**31.
But that is over 2,000 million, not 100-200 million, so I would wonder if you have
a hot-running laptop that decides to slow down when it gets to a certain temp. Or maybe
your fan/heatsink is clogged with dust, or the heatsink isn't making good thermal contact
with the CPU?
2**31-1 2147483647L

That says "L" for long, because it became long before the 1 was subtracted.
2**30-1+2**30 2147483647

One way to avoid that overflow, by doing in another order
x = 2**30-1+2**30
x 2147483647

Simulating your code
x += 1
x

2147483648L

Now you are in the long-integer representation mode, which is not as fast as int,
and as the size of the number grows, it will get gradually get slower, because it
will be manipulating larger and larger representations . A rough measure of how big
would be len('%x'%x). But as mentioned, I don't think this is the problem.
To see that kind of effect, calculate pi to thousands of decimals or monster
factorials or something like that ;-)

Regards,
Bengt Richter
Jul 18 '05 #4
tr***@trevp.net (Trevor Perrin) wrote in message news:<47******* *************** ****@posting.go ogle.com>...
After running a simple loop around 100-200 million times, there's a
speed drop of about a factor of 7. This happens within several
minutes (on a 1.7 Ghz machine), so it's not that hard to see.


Never mind, Python's just too speedy for my laptop - the CPU's
overheating, as Bengt thought. I'll have to switch to VB or something
;-) Thanks, people..

Trevor
Jul 18 '05 #5
Trevor Perrin wrote:
Never mind, Python's just too speedy for my laptop - the CPU's
overheating, as Bengt thought.


Oh, great. As if there weren't enough tricky things to take into
account when benchmarking on modern machines - we now have to take
care to pre-heat the CPU, too!

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

Jul 18 '05 #6

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

Similar topics

3
1622
by: Nicke | last post by:
Hi, I've used vb.net for 3 hours(!) now and have probably a very easy question. In my VB6 programs I use For Each Cell in Range... very often which not seems to work in vb.net. Ex. Dim Cell As Excel.Range Dim xl as Excel.Application
8
2053
by: Sebastian Werner | last post by:
Howdy, I currently develop the javascript toolkit qooxdoo (http://qooxdoo.sourceforge.net), some of you heard it already. We have discovered a slowdown on Internet Explorers performance when creating objects with some data and store them in a global object registry. It take some time to get this example extracted from our codebase. The attached file (take a look at it please) shows the exact problem. The time for each object increases...
0
1217
by: vojtech | last post by:
Hi, I would like to remark on a problem described by Stephen Livesey almost 3 years ago, about the slowdown he had experienced with an upload of several millions of rows. http://www.geocrawler.com/mail/thread.php3?subject=%5BGENERAL%5D+Slowdown+problem+when+writing+1.7million+records&list=12
22
2365
by: Bradley | last post by:
Has anyone else noticed this problem? I converted the back-end to A2000 and the performance problem was fixed. We supply a 97 and 2000 version of our software so we kept the backend in A97 to make upgrading simple for users. We've done it like that for years but a new client has been having severe performance issues... solved by converting the backend to 2000. -- regards, Bradley
45
7486
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286) word size was 16 bits same as an integer. Now with a 32 bit CPU I would have expected the long to be faster as it's the same size as the CPU's word size so wouldn't need sawing in half like a magician's assistant to calculate on like an...
3
1659
by: tac-tics | last post by:
I have an application written in jython which has to process a number of records. It runs fine until it gets to about 666 records (and maybe that's a sign), and then, it's performance and responsiveness goes down the toilet. It looks like it's running out of memory and is being forced to use extended memory, but I do not know enough about the language to figure out where this is happening. It will eventually finish the task, but the window...
20
2856
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1 then
0
885
by: guillaume weymeskirch | last post by:
Hello everybody, To test the python 2.5 garbage collector, I wrote a trivial script allocating dummy objects of various sizes, then forgetting them in a loop. The garbage collector seems working well, limiting the memory used. But I've noticed a near linear slowdown of the execution : after a few minutes - and several millions of allocated and freed objects, each iteration take more and more time to execute.
0
1250
by: skip | last post by:
guillaumeBut I've noticed a near linear slowdown of the execution : guillaumeafter a few minutes - and several millions of allocated and guillaumefreed objects, each iteration take more and more time to guillaumeexecute. I ran your script on my Mac and let it go for over an hour. The times printed near the end were not that different than the times near the start. First few prints:
0
9703
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...
1
10300
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
10069
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
9127
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...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
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();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
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
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.