-
-
Save xudifsd/6265396 to your computer and use it in GitHub Desktop.
there are two method for get timestamp, first is just use system call, second is use system call for the first time, and then start a background thread and let system to schedule a inc function every second.
from this test, we can see that the second method is more efficient.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import threading, time | |
time1 = 0 | |
def get_time1(): | |
return time1 | |
def get_time2(): | |
return int(time.time())#this involve system call | |
def periodic_inc_time(): | |
global time1 | |
time1 += 1 | |
def test1(times): | |
time1 = int(time.time()) | |
threading.Timer(1, periodic_inc_time).start() | |
for i in range(times): | |
get_time1() | |
def test2(times): | |
for i in range(times): | |
get_time2() | |
if __name__ == '__main__': | |
times = 1000000 | |
start = time.time() | |
test2(times) | |
end = time.time() | |
print "for test2 " + str(end - start)#for test2 0.422958850861 | |
#because test1 will start a background thread, we test it lastly | |
start = time.time() | |
test1(times) | |
end = time.time() | |
print "for test1 " + str(end - start)#for test1 0.156961917877 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment