开发者

Timing function in python not giving accurate result

开发者 https://www.devze.com 2023-01-05 10:15 出处:网络
i have developed a scheme for signcryption, i want to test the time taken for modularexponentiation. i am using the below code for signcryption part

i have developed a scheme for signcryption, i want to test the time taken for modular exponentiation. i am using the below code for signcryption part

start = time.clock()
gamma  =    pow(g , x, p)
pr开发者_如何转开发int ('The value of gamma is : '),gamma
Time_signcrypt = time.clock() - start

and for unsigncryption part i am calculating the time taken with this line of code

start = time.clock()
seed =  (XA + x - XA) 
gamma_new = pow(g , seed, p)
Time_new_gamma = time.clock() - start

The problem is using the same values, the results i get from both the timing function is different.

Signcryption values: 
0.035299674 
0.025940017 

Unsigncryption values: 
0.019342944
0.01727206

The values should be same as the same function is applied at both ends with same parameters. Another important things is that in unsigncryption part , one step is additional but still the time taken is less than the signcryption part. I cant get it what is wrong i have tested almost 35 times and the results vary most of the times :(

Please advice where am i going wrong ?


To time methods, run them many times until the cumulated time is at least 10 seconds, then divide the time by the number of runs.

Otherwise, the timing will be very inaccurate because of various reasons:

  1. Other processes which get the CPU
  2. Interrupts running in the background
  3. Thermal effects
  4. Cosmic radiation
  5. You get the idea.. ;-)


There's a timeit module for doing exactly this kind of thing. It runs your code multiple times (1 million by default) and reports the stats for that run. Much more accurate than trying to time a single run, where your code can be subject to all sorts of issues.


Because CPU's are constantly scheduling between different processes, the same piece of code will take a different time, every time it's executed.

The first function will be in general slower, because of the print statement, which takes "quite some" time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号