0

In the following code when I run following code why it returns me different time. It should be same.

`for i in range(2):
    start=time.time()
    print(start)
    a= np.arange(10).reshape(2,5)
    print(a)
    end=time.time()
    print(end)
    dd=datetime.timedelta(seconds = end-start)

    print("time diff: ",dd)`
user10163680
  • 253
  • 2
  • 11
  • It's unclear what you are asking for. Every OS that runs multiple processes has a scheduler that affects runtimes. Does this give you something to reword your question? – alvits Aug 14 '18 at 00:58
  • I want to ask that if i run for loop twice as in above code and suppose it takes 30 seconds for its completion .Then if I run it again it should take 30 seconds for its completion. dd value should be same for both case – user10163680 Aug 14 '18 at 01:06
  • No. It will not be the same. It will just be coincidence if they are the same. During those loop, some other processes could be taking turn in using the CPU. They don't get interrupted at the same exact moment for the same exact duration. – alvits Aug 14 '18 at 01:07
  • other processes? If I only run a single programe at a time – user10163680 Aug 14 '18 at 01:18
  • Are you telling me that your computer isn't running disk management, disk drivers, cron daemon, smtp, other linux services, etc. – alvits Aug 14 '18 at 01:20
  • by single programe I mean a single python programe – user10163680 Aug 14 '18 at 01:22
  • should I use time.clock to get the desired result? – user10163680 Aug 14 '18 at 01:22
  • Possible duplicate of [Python's time.clock() vs. time.time() accuracy?](https://stackoverflow.com/questions/85451/pythons-time-clock-vs-time-time-accuracy) – alvits Aug 14 '18 at 01:48

1 Answers1

0

set priority on process the most times ill be ok there enjoy

import time 
import numpy as np
import datetime
import psutil, os
p = psutil.Process(os.getpid())
p.nice(psutil.REALTIME_PRIORITY_CLASS)
for i in range(2):
    start=time.time()
    print(start)
    a= np.arange(10).reshape(2,5)
    print(a)
    end=time.time()
    print(end)
    dd=datetime.timedelta(seconds = end-start)
    print("time diff: ",dd)