tags:

views:

53

answers:

4

Dear All 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)
print ('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 ?

+3  A: 

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.. ;-)
Aaron Digulla
and can you please provide me the reason for this as i am calculating time for only a simple pow () function, its just one line and same thing is happening in unsigncryption part, still both the outputs vary. also the values of signcryption and unsigncryption vary alot but the values within each signcryption/unsigncryption part are closer.I have been testing the values since morning now n no luck :S :(
fahad
ok i have an idea now , thanks for the update :)
fahad
+1  A: 

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.

Peter Smit
+2  A: 

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.

pycruft
A: 

I asked a similar question about accurate time stamping in Python which may be of use.

Jon Cage