performance - Python - Fastest way to generate list of random colours with fixed alpha -


so i'm looking generate large list of approximately 332 million colours (tuples 4 values - r,g,b,a) in python, fixed alpha value of 0.6. need duplicate every colour in row below (i.e. end 664 million rows - 332 million distinct colours.

i have tried , tested many methods have concluded fastest far use numpy to:

  1. create array of length 332 million containing 3 random float values per row using numpy random
  2. create second array of same length values [0, 0, 0, 0.6] per row using numpy tile
  3. copy values first array first 3 values per row of second array
  4. use numpy repeat function repeat every row onto new inserted row below (interleave array duplicates of previous row)

the code takes approx. 574 seconds or 10 minutes , is:

import time import numpy np  t1 = time.time()  randomnos = np.random.random_sample((332000000, 3))  trans = np.tile([0,0,0,0.6],(332000000, 1)) trans[:,:-1] = randomnos colorarray = np.repeat(trans, 2, axis=0)  t2 = time.time()  totaltime = t2 - t1  print "time taken = %f seconds" % totaltime 

now don't need generate random numbers @ runtime concluded run code once , save file (using numpy save), read file @ runtime using numpy load function, though considerably faster still slow me taking 106 seconds or approximately 2 minutes.

i think there must faster way generate random colours @ runtime?

the desired output like:

array = [[ 0.98112321  0.38567094  0.2430455   0.6       ]  [ 0.98112321  0.38567094  0.2430455   0.6       ]  [ 0.54728619  0.97823465  0.86675383  0.6       ]  [ 0.54728619  0.97823465  0.86675383  0.6       ]  ...,   [ 0.15047076  0.55844066  0.79842858  0.6       ]  [ 0.15047076  0.55844066  0.79842858  0.6       ]  [ 0.53637406  0.52150776  0.44890727  0.6       ]  [ 0.53637406  0.52150776  0.44890727  0.6       ]] 

i'm bit confused code you've shown. seem doing things in round-about way, , may misunderstanding want.

however, understand it, want:

import numpy np  colorarray = np.random.random_sample((332000000, 4)) colorarray[:, -1] = 0.6 colorarray = np.vstack([colorarray, colorarray]) 

this takes ~30 seconds me.

or, if wanted optimize things as possible, can shave off second or two:

num = 332000000 colorarray = np.empty((2 * num, 4), dtype=float) colorarray[:num, :3] = np.random.random_sample((num, 3)) colorarray[:num, -1] = 0.6 colorarray[num:] = colorarray[:num] 

this takes ~28 seconds.


edit: misunderstood wanted do. (my fault, not yours. had nice, stand-alone, runnable example. didn't read closely enough!)

to "interleave" values, can use simple slicing trick. starting more optimized example:

num = 332000000 colorarray = np.empty((2 * num, 4), dtype=float) colorarray[::2, :3] = np.random.random_sample((num, 3)) colorarray[::2, -1] = 0.6 colorarray[1::2] = colorarray[::2] 

this takes bit longer run (~42 seconds), gives values want.

numpy.repeat, using, turns out bit faster assigning slices, in case. therefore, can things down ~34 seconds with:

colorarray = np.empty((num, 4), dtype=float) colorarray[:, :3] = np.random.random_sample((num, 3)) colorarray[:, -1] = 0.6 colorarray = np.repeat(colorarray, 2, axis=0) 

edit 2

and @ end of this, though benchmark original code... it's identical in speed final version. seems comment on example being "roundabout" off-base!


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -