I want to generate a very long list of random two dimensional coordinates (floats) between 0.0
and 1.0
.
Do you know a faster code than this (on my computer it takes about 4.1 sec
for 10**7
coordinates)?:
coordinates = (np.random.randint(0,10,(10**7,2))/10.).tolist()
For this number I can wait, but what should I do when the number of coordinates is 10**9
?
Timing for 10**7
:
import numpy as np
import timeit
def createCoordinates(num_coord):
return (np.random.randint(0, 10, (num_coord, 2))/10.).tolist()
def checkElapsedTime(num_runs):
t_elapsed = np.empty(num_runs, dtype=np.float)
for i in range(num_runs):
t_start = timeit.default_timer()
coordinates_2d = createCoordinates(10**7)
t_elapsed[i] = timeit.default_timer()-t_start
print('run: %2d, time_elapsed = %4.3f sec' % (i, t_elapsed[i]))
print('(mean \u00B1 standard deviation): elapsed time = %4.3f sec \u00B1 %5.4f sec' % \
(np.mean(t_elapsed), np.std(t_elapsed)))
checkElapsedTime(10)
run: 0, time_elapsed = 5.335 sec
run: 1, time_elapsed = 4.511 sec
run: 2, time_elapsed = 1.907 sec
run: 3, time_elapsed = 4.159 sec
run: 4, time_elapsed = 4.193 sec
run: 5, time_elapsed = 4.239 sec
run: 6, time_elapsed = 4.209 sec
run: 7, time_elapsed = 4.189 sec
run: 8, time_elapsed = 4.273 sec
run: 9, time_elapsed = 4.329 sec
(mean ± stdandard deviation): elapsed time = 4.134 sec ± 0.8139 sec
Aucun commentaire:
Enregistrer un commentaire