mirror of
https://github.com/rasbt/python_reference.git
synced 2025-01-31 05:33:47 +00:00
1st commit
This commit is contained in:
parent
ff77be547e
commit
3198a2580d
18
cpu_time.py
Normal file
18
cpu_time.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# sr 10/29/13
|
||||||
|
# Calculates elapsed CPU time in seconds as float.
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
start_time = time.clock()
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
while i < 10000000:
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
elapsed_time = time.clock() - start_time
|
||||||
|
print "Time elapsed: {} seconds".format(elapsed_time)
|
||||||
|
|
||||||
|
# prints "Time elapsed: 1.06 seconds"
|
||||||
|
# on 4 x 2.80 Ghz Intel Xeon, 6 Gb RAM
|
||||||
|
|
||||||
|
|
23
pickle_module.py
Normal file
23
pickle_module.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# sr 10/29/13
|
||||||
|
# The pickle module converts Python objects into byte streams
|
||||||
|
# to save them as a file on your drive for re-use.
|
||||||
|
#
|
||||||
|
# module documentation http://docs.python.org/2/library/pickle.html
|
||||||
|
|
||||||
|
import pickle
|
||||||
|
|
||||||
|
#### Generate some object
|
||||||
|
my_dict = dict()
|
||||||
|
for i in range(1,1000):
|
||||||
|
my_dict[i] = "some text"
|
||||||
|
|
||||||
|
#### Save object to file
|
||||||
|
pickle_out = open('my_file.pkl', 'wb')
|
||||||
|
pickle.dump(my_dict, pickle_out)
|
||||||
|
pickle_out.close()
|
||||||
|
|
||||||
|
#### Load object from file
|
||||||
|
my_object_file = open('my_file.pkl', 'rb')
|
||||||
|
my_dict = pickle.load(my_object_file)
|
||||||
|
my_object_file.close()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user