top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to get time (milisecond) of a python IO execution

+2 votes
581 views

I am trying to test measure some IO execution in milliseconds , but bit confuse about best method achive that under windows 7. I am using following code but not sure if its best or correct way since i have two different results, which one should i take as results and which is best way. Please advice

code exec results 
100000000 loops, best of 3: 0.00925 usec per loop
3.827947176762156

run code
 import timeit ,time
 import datetime
 import os

 def main():    
 os.path.getsize("c:/video-2011-09-09-09-32-29.mp4")  
 if __name__ == '__main__':    
 t = timeit.Timer('main()')    
 print (t.timeit(1))
posted Sep 15, 2013 by Anderson

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

I'm not familiar with how Windows works, but I wouldn't be surprised if it caches directory information. (Unix systems certainly would.) You probably aren't really doing much actual I/O to get the size of a file after the first run. Also, you probably want to subtract the time it takes to execute a no-op function:

 def noop(): pass

to eliminate the overhead of calling your main function.

answer Sep 15, 2013 by Dewang Chaudhary
+1 vote

Measuring any performance can be tricky, and I/O in particular. Windows will cache the recently used file information, at a few levels. So if you do the same thing repeatedly, you'll get an unnaturally low average
time.

if you really need to measure how long it takes to get a file size, you would need to write your own, very controlled, test code. For example, there is an APi call that tells Windows to flush its disk caching logic. However, it doesn't necessarily flush the buffers that the drive itself keeps. Worse, in order to run your program, you have to do some disk I/O, and if that happens to use some of the same sectors as the thing you're timing, you're biasing the results.

If you have multiple partitions, you could arrange that the file in question is the only thing your system uses on that partition, to minimize the likelihood that something else has primed the pump. But realize also that once you're measuring the real thing, speed of exeution will vary enormously depending on the speed of the drive, the layout of the directories, and the fragmentation of the disk.

answer Sep 15, 2013 by Mandeep Sehgal
Similar Questions
0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 try:
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
 break

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..

0 votes

This is my dilemma, Im trying to get the generated JSON file using the bing api search.

This is the code that Im executing from inside the shell:
http://bin.cakephp.org/view/460660617 [1]

The port doesnt matter to me. Thoughts?

+1 vote

I need to get 32 bit binary equivalent of a decimal and need to change the 0's to 1's and 1's to 0's

For Example
if the input is 2 
Output should be:
the 32bit equivalent of 2 :0000 0000 0000 0000 0000 0000 0000 0010
and the 1's compliment is:1111 1111 1111 1111 1111 1111 1111 1101

is there any pre-defined function to get the above results in python??

+1 vote

I want to show simple dots while my program copies the files. I have found the code like:

for i in range(10):
 print '.',
 time.sleep(1)

But this will execute ten times as it is predefined and the task to copy will execute after or before this loop based on the location I have placed my line to copy the files using shutil.copy().

I want that the files should be copied and in parallel the progress should be shown and when the files are copied, the progress should exit.

...