473,785 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Interesting timing issue I noticed

On 4/15/08, Daniel Fetchinson <fe********@goo glemail.comwrot e:
Can I then simply ignore the time data then? I do see better performance
obviously the smaller the box is, but I guess my issues is how seriously
to
take all this data. Because I can't claim "performanc e improvement" if
there
isn't really much of an improvement.

On Tue, Apr 15, 2008 at 11:04 PM, Daniel Fetchinson <
fe********@goog lemail.comwrote:
I've written up a stripped down version of the code. I apologize for
the bad
coding; I am in a bit of a hurry.

import random
import sys
import time

sizeX = 320
sizeY = 240
borderX = 20
borderY = 20

# generates a zero matrix
def generate_zero() :
matrix = [[0 for y in range(sizeY)] for x in range(sizeX)]
return matrix
# fills zero matrix
def fill_matrix(in_ mat):
mat = in_mat
for x in range(sizeX):
for y in range(sizeY):
mat[x][y] = random.randint( 1, 100)
return mat
############### ############### ############### ############### ##########
# COMPUTES ONLY A PART OF THE ARRAY
def back_diff_one(b ack_array, fore_array, box):
diff_array = generate_zero()

start = time.time()
for x in range(sizeX):
for y in range(borderY):
diff_array[x][y] = back_array[x][y] - fore_array[x][y]
for y in range((sizeY - borderY), sizeY):
diff_array[x][y] = back_array[x][y] - fore_array[x][y]
for y in range(borderY, (sizeY - borderY)):
for x in range(borderX):
diff_array[x][y] = back_array[x][y] - fore_array[x][y]
for x in range((sizeX - borderX), sizeX):
diff_array[x][y] = back_array[x][y] - fore_array[x][y]

# tracks object
if (len(box) != 0):
for x in range(box[0], box[2]):
for y in range(box[1], box[3]):
diff_array[x][y] = back_array[x][y] - fore_array[x][y]
print "time one inside = " + str(time.time() - start)
return diff_array
############### ############### ############### ############### ##########
# COMPUTES EVERY ELEMENT IN THE ARRAY
def back_diff_two(b ack_array, fore_array):
diff_array = generate_zero()
start = time.time()
for y in range(sizeY):
for x in range(sizeX):
diff_array[x][y] = back_array[x][y] - fore_array[x][y]
end = time.time()
print "time two inside = " + str(end - start)
return diff_array
############### ############### ############### ############### ##########
# CODE TO TEST BOTH FUNCTIONS
back = fill_matrix(gen erate_zero())
fore = fill_matrix(gen erate_zero())
box = [20, 20, 268, 240]
start1 = time.time()
diff1 = back_diff_one(b ack, fore, box)
print "time one outside = " + str(time.time() - start1)
start2 = time.time()
diff2 = back_diff_two(b ack, fore)
print "time one outside = " + str(time.time() - start2)

Here are some results from several test runs:

time one inside = 0.0780000686646
time one outside = 0.125
time two inside = 0.0780000686646
time two outside = 0.141000032425
>>============= =============== ==== RESTART
=============== =============== ==
>>>
time one inside = 0.0629999637604
time one outside = 0.125
time two inside = 0.0789999961853
time two outside = 0.125
>>============= =============== ==== RESTART
=============== =============== ==
>>>
time one inside = 0.0620000362396
time one outside = 0.139999866486
time two inside = 0.0780000686646
time two outside = 0.125
>>============= =============== ==== RESTART
=============== =============== ==
>>>
time one inside = 0.0780000686646
time one outside = 0.172000169754
time two inside = 0.0789999961853
time two outside = 0.125
>>============= =============== ==== RESTART
=============== =============== ==
>>>
time one inside = 0.0780000686646
time one outside = 0.125
time two inside = 0.0780000686646
time two outside = 0.125
>>============= =============== ==== RESTART
=============== =============== ==
>>>
time one inside = 0.0620000362396
time one outside = 0.155999898911
time two inside = 0.0780000686646
time two outside = 0.125
>>============= =============== ==== RESTART
=============== =============== ==
>>>
time one inside = 0.077999830246
time one outside = 0.125
time two inside = 0.077999830246
time two outside = 0.125
>>============= =============== ==== RESTART
=============== =============== ==
>>>
time one inside = 0.0780000686646
time one outside = 0.171000003815
time two inside = 0.077999830246
time two outside = 0.125
>>============= =============== ==== RESTART
=============== =============== ==
>>>
time one inside = 0.0629999637604
time one outside = 0.18799996376
time two inside = 0.0620000362396
time two outside = 0.125

Why is a large percentage of the time, the execution time for the
(ostensibly smaller) first loop is actually equal to or LARGER than
the
second?
>
>
First of all, your method of timing is not the best. Use the timeit
module instead: http://docs.python.org/lib/module-timeit.html
>
Second of all the number of subtractions is not that different between
the two variants of your functions. back_diff_one does 75360
subtractions per call while back_diff_two does 76800, these two
numbers are almost the same. It's true that back_diff_one first only
calculates a part of the arrays but after "# tracks object" you do a
bunch of more substractions that will make up the total count.
>
HTH,
Daniel
>

Please keep the discussion on the list.

Yes, if I were you I would discard your original timing data and redo
it using the timeit module. Whatever that gives should be reliable and
you can start from there. In any case your two functions are doing
roughly the same number of operations.

HTH,
Daniel

BTW, using the following

############### ############### ############### ############### ##########
# CODE TO TEST BOTH FUNCTIONS
back = fill_matrix(gen erate_zero())
fore = fill_matrix(gen erate_zero())
box = [20, 20, 268, 240]

def test1( ):
diff1 = back_diff_one(b ack, fore, box)

def test2( ):
diff2 = back_diff_two(b ack, fore)

if __name__=='__ma in__':
from timeit import Timer
t = Timer("test1( )", "from __main__ import test1")
print t.timeit( 50 )
t = Timer("test2( )", "from __main__ import test2")
print t.timeit( 50 )
and removing all your timing code from the two functions gives

1.63772082329
1.82889485359

which is consistent with your expectation that the version that
computes only a part of the image runs faster. But only marginally,
which is again consistent with the fact that the number of operations
is only marginally different.

HTH,
Daniel
Jun 27 '08 #1
0 1946

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

Similar topics

3
8133
by: Russell | last post by:
I have a quirky issue that I believe involves timing and only 2 hairs left to pull. I have a modal dialog that is an IFrame. The IFrame contains another window - which contains the appropriate title. I am trying to change the title of the IFrame window to be that of the contained window title. If I uncomment the alert statement below - the title change works. Comment out the alert - and - no title change.
3
2397
by: pbbriggs | last post by:
I will try to be as descriptive as possible, but this error has me stumped and I am not sure what relevant info to include.... I am running Access XP on a Windows XP machine. I initially began developing an app in Access 2000 but converted it several months ago to XP. So the app is now Access XP format also. My app is crashing on a regular basis, with the totally non-descript Microsoft error (Microsoft Access has encountered a...
7
2196
by: Jacob Barnett | last post by:
Access 2000 main report does not display subreport data in preview and may or may not print... sometimes. Usually, the entire report is fine. The behavior does not seem to depend on particular records or data and I have not found a reliable way to reproduce the behavior. The subreport can always be generated independently and always prints. There is data in both the main report and the subreport. The link child and master fields are...
4
2645
by: Brian Hanson | last post by:
Hi, I have a problem that is sporadic and am thinking it may come down to a timing issue. I have an asp.net (vb) app that used the system.io.file.copy method to copy a .pdf file from a network drive to the local server drive. From there I use that .pdf file to create another .pdf file using a tool from ActivePDF. The problem I have is that one out of 8 or 10 times I get an error: Access to the path "c:\ActivePDF\watever.pdf" is...
5
3665
by: Richard Charts | last post by:
I am building a simulator that needs to send out 2 messages on a tcp connection each at a rate of 1 packet / 50ms. Right now it kind of works in that it is sending 8 (4 of each) at 200ms. Is there a way to force the packets to at the least go out every 50 ms? If 1 of each is combined is fine, but 4 of each at a time is a problem. Are there any more options I can set to improve this? Right now my socket is setup as : serverSocket = new...
27
2340
by: Frederick Gotham | last post by:
I thought it might be interesting to share experiences of tracking down a subtle or mysterious bug. I myself haven't much experience with tracking down bugs, but there's one in particular which comes to mind. I was writing usable which dealt with strings. As per usual with my code, I made it efficient to the extreme. One thing I did was replace, where possible, any usages of "strlen" with something like: struct PtrAndLen { char *p;
4
3166
by: Nebulus | last post by:
We've got a website that's designed in classic ASP. While it's a good product, the original design was badly done, and I've inherited a monster. At some point last week, users began calling in and complaining that their sessions were timing out after minutes instead of the 120 minute session timeout that's set in the global.asa Now, at any given time, we have about 100 people logged into the site. I did perform a check, and at any...
2
1548
by: julie.siebel | last post by:
Google apparently ate my original post to this (grr) so this'll be a bit more vague than the initial post, but...*sigh*. Javascript is not my forte, and I apologize for the acky-ness of the code. I'm working on a page for a travel company. There's way too much going on with it/on it, but I'm kind of stuck at this point - the client loves it, so I have to solve this problem.
0
125
by: Daniel Fetchinson | last post by:
Can I then simply ignore the time data then? I do see better performance Please keep the discussion on the list. Yes, if I were you I would discard your original timing data and redo it using the timeit module. Whatever that gives should be reliable and you can start from there. In any case your two functions are doing roughly the same number of operations. HTH,
0
10152
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
10092
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
8974
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
7500
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
6740
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
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
3
2880
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.