Re: [julia-users] Fastest method to create a matrix of random integers

2014-03-25 Thread David P. Sanders


El lunes, 24 de marzo de 2014 17:55:39 UTC-6, Jacob Quinn escribió:
>
> How about
>
> In  [186]: @time rand(-1:2:1,1,1);
> elapsed time: 2.29940616 seconds (80224 bytes allocated)
>
> No need for an extra function. This uses a range from -1 to 1 with a step 
> size of 2 so you only get those two numbers
>

OK, thanks, I didn't know you could do that. It's still twice as slow as 
elementwise_random_block though
(the idea being that it's faster to generate random numbers in a block and 
store them than generate them one by one); see below.
I also did not specify correctly that I will also need to generate the 
numbers with different probabilities (not necesarily 50--50).

Surprisingly, `elementwise` is sometimes fast (0.8 seconds) and sometimes 
slow (5 seconds)...

julia> function using_rand(L)
   rand(-1:2:1, L, L)
   end
using_rand (generic function with 1 method)


julia> L = 1
1

julia> @time rand_pm1_matrix(L);
elapsed time: 1.02277517 seconds (1812500448 bytes allocated)

julia> @time elementwise(L);
elapsed time: 5.06254 seconds (10176 bytes allocated)

julia> @time elementwise_random_block(L);
elapsed time: 0.86818943 seconds (90288 bytes allocated)

julia> @time using_rand(L);
elapsed time: 1.939470921 seconds (80160 bytes allocated)



>
> On Mon, Mar 24, 2014 at 7:21 PM, David P. Sanders 
> 
> > wrote:
>
>> Hi,
>>
>> What is the fastest way to create a matrix containing random integers: 1 
>> or -1 with equal probability?
>> I have tried the following options but was wondering if I am missing 
>> something.
>> (I also need 1, 0 or -1 with different probabilities later, which is why 
>> I don't just use a boolean random matrix.)
>>  
>> Thanks,
>> David.
>>
>> julia> function rand_pm1_matrix(L)
>>matrix::Array{Int8, 2} = 2 .* randbool(L,L) .- 1
>>end
>> rand_pm1_matrix (generic function with 1 method)
>>
>> julia> function elementwise(L)
>>A = Array(Int8, (L, L))
>>for i in 1:length(A)
>>if rand() < 0.5
>>A[i] = -1
>>else
>>A[i] = 1
>>end
>>end
>>A
>>end
>> elementwise (generic function with 1 method)
>>
>> julia> function elementwise_random_block(L)
>>r = rand(L, L)
>>A = Array(Int8, (L, L))
>>for i in 1:length(A)
>>if r[i] < 0.5
>>A[i] = -1
>>else
>>A[i] = 1
>>end
>>end
>>A
>>end
>> elementwise_random_block (generic function with 1 method)
>>
>> julia> L = 10
>> 10
>>
>> julia> rand_pm1_matrix(L);
>>
>> julia> elementwise(L);
>>
>> julia> elementwise_random_block(L);
>>
>> julia>
>>
>> julia> L = 100
>> 100
>>
>> julia> @time rand_pm1_matrix(L);
>> elapsed time: 6.5661e-5 seconds (188588 bytes allocated)
>>  
>> julia> @time elementwise(L);
>> elapsed time: 0.001172332 seconds (10160 bytes allocated)
>>
>> julia> @time elementwise_random_block(L);
>> elapsed time: 0.000131455 seconds (90240 bytes allocated)
>>
>> julia>
>>
>> julia> L = 1000
>> 1000
>>
>> julia> @time rand_pm1_matrix(L);
>> elapsed time: 0.013428217 seconds (18125456 bytes allocated)
>>
>> julia> @time elementwise(L);
>> elapsed time: 0.074257183 seconds (1000176 bytes allocated)
>>
>> julia> @time elementwise_random_block(L);
>> elapsed time: 0.007721683 seconds (9000288 bytes allocated)
>>
>> julia>
>>
>> julia> L = 1
>> 1
>>
>> julia> @time rand_pm1_matrix(L);
>> elapsed time: 2.75006866 seconds (1812500448 bytes allocated)
>>
>> julia> @time elementwise(L);
>> elapsed time: 6.727148454 seconds (10176 bytes allocated)
>>
>> julia> @time elementwise_random_block(L);
>> elapsed time: 2.819847794 seconds (90288 bytes allocated)
>>
>> \
>>
>
>

Re: [julia-users] Fastest method to create a matrix of random integers

2014-03-24 Thread Jacob Quinn
How about

In  [186]: @time rand(-1:2:1,1,1);
elapsed time: 2.29940616 seconds (80224 bytes allocated)

No need for an extra function. This uses a range from -1 to 1 with a step
size of 2 so you only get those two numbers.

-Jacob


On Mon, Mar 24, 2014 at 7:21 PM, David P. Sanders wrote:

> Hi,
>
> What is the fastest way to create a matrix containing random integers: 1
> or -1 with equal probability?
> I have tried the following options but was wondering if I am missing
> something.
> (I also need 1, 0 or -1 with different probabilities later, which is why I
> don't just use a boolean random matrix.)
>
> Thanks,
> David.
>
> julia> function rand_pm1_matrix(L)
>matrix::Array{Int8, 2} = 2 .* randbool(L,L) .- 1
>end
> rand_pm1_matrix (generic function with 1 method)
>
> julia> function elementwise(L)
>A = Array(Int8, (L, L))
>for i in 1:length(A)
>if rand() < 0.5
>A[i] = -1
>else
>A[i] = 1
>end
>end
>A
>end
> elementwise (generic function with 1 method)
>
> julia> function elementwise_random_block(L)
>r = rand(L, L)
>A = Array(Int8, (L, L))
>for i in 1:length(A)
>if r[i] < 0.5
>A[i] = -1
>else
>A[i] = 1
>end
>end
>A
>end
> elementwise_random_block (generic function with 1 method)
>
> julia> L = 10
> 10
>
> julia> rand_pm1_matrix(L);
>
> julia> elementwise(L);
>
> julia> elementwise_random_block(L);
>
> julia>
>
> julia> L = 100
> 100
>
> julia> @time rand_pm1_matrix(L);
> elapsed time: 6.5661e-5 seconds (188588 bytes allocated)
>
> julia> @time elementwise(L);
> elapsed time: 0.001172332 seconds (10160 bytes allocated)
>
> julia> @time elementwise_random_block(L);
> elapsed time: 0.000131455 seconds (90240 bytes allocated)
>
> julia>
>
> julia> L = 1000
> 1000
>
> julia> @time rand_pm1_matrix(L);
> elapsed time: 0.013428217 seconds (18125456 bytes allocated)
>
> julia> @time elementwise(L);
> elapsed time: 0.074257183 seconds (1000176 bytes allocated)
>
> julia> @time elementwise_random_block(L);
> elapsed time: 0.007721683 seconds (9000288 bytes allocated)
>
> julia>
>
> julia> L = 1
> 1
>
> julia> @time rand_pm1_matrix(L);
> elapsed time: 2.75006866 seconds (1812500448 bytes allocated)
>
> julia> @time elementwise(L);
> elapsed time: 6.727148454 seconds (10176 bytes allocated)
>
> julia> @time elementwise_random_block(L);
> elapsed time: 2.819847794 seconds (90288 bytes allocated)
>
> \
>


[julia-users] Fastest method to create a matrix of random integers

2014-03-24 Thread David P. Sanders
Hi,

What is the fastest way to create a matrix containing random integers: 1 or 
-1 with equal probability?
I have tried the following options but was wondering if I am missing 
something.
(I also need 1, 0 or -1 with different probabilities later, which is why I 
don't just use a boolean random matrix.)

Thanks,
David.

julia> function rand_pm1_matrix(L)
   matrix::Array{Int8, 2} = 2 .* randbool(L,L) .- 1
   end
rand_pm1_matrix (generic function with 1 method)

julia> function elementwise(L)
   A = Array(Int8, (L, L))
   for i in 1:length(A)
   if rand() < 0.5
   A[i] = -1
   else
   A[i] = 1
   end
   end
   A
   end
elementwise (generic function with 1 method)

julia> function elementwise_random_block(L)
   r = rand(L, L)
   A = Array(Int8, (L, L))
   for i in 1:length(A)
   if r[i] < 0.5
   A[i] = -1
   else
   A[i] = 1
   end
   end
   A
   end
elementwise_random_block (generic function with 1 method)

julia> L = 10
10

julia> rand_pm1_matrix(L);

julia> elementwise(L);

julia> elementwise_random_block(L);

julia>

julia> L = 100
100

julia> @time rand_pm1_matrix(L);
elapsed time: 6.5661e-5 seconds (188588 bytes allocated)

julia> @time elementwise(L);
elapsed time: 0.001172332 seconds (10160 bytes allocated)

julia> @time elementwise_random_block(L);
elapsed time: 0.000131455 seconds (90240 bytes allocated)

julia>

julia> L = 1000
1000

julia> @time rand_pm1_matrix(L);
elapsed time: 0.013428217 seconds (18125456 bytes allocated)

julia> @time elementwise(L);
elapsed time: 0.074257183 seconds (1000176 bytes allocated)

julia> @time elementwise_random_block(L);
elapsed time: 0.007721683 seconds (9000288 bytes allocated)

julia>

julia> L = 1
1

julia> @time rand_pm1_matrix(L);
elapsed time: 2.75006866 seconds (1812500448 bytes allocated)

julia> @time elementwise(L);
elapsed time: 6.727148454 seconds (10176 bytes allocated)

julia> @time elementwise_random_block(L);
elapsed time: 2.819847794 seconds (90288 bytes allocated)

\