473,387 Members | 1,540 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,387 software developers and data experts.

Re: Interesting timing issue I noticed

On 4/15/08, Daniel Fetchinson <fe********@googlemail.comwrote:
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 "performance improvement" if
there
isn't really much of an improvement.

On Tue, Apr 15, 2008 at 11:04 PM, Daniel Fetchinson <
fe********@googlemail.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(back_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(back_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(generate_zero())
fore = fill_matrix(generate_zero())
box = [20, 20, 268, 240]
start1 = time.time()
diff1 = back_diff_one(back, fore, box)
print "time one outside = " + str(time.time() - start1)
start2 = time.time()
diff2 = back_diff_two(back, 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(generate_zero())
fore = fill_matrix(generate_zero())
box = [20, 20, 268, 240]

def test1( ):
diff1 = back_diff_one(back, fore, box)

def test2( ):
diff2 = back_diff_two(back, fore)

if __name__=='__main__':
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 1901

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

Similar topics

3
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...
3
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...
7
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...
4
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...
5
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...
27
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...
4
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...
2
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...
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.