0.5 new generators syntax question

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

0.5 new generators syntax question

Jesse Jaanila
Hi,

I was experimenting with the new 0.5 features and they are great! But to my surprise,
the generator syntax doesn't work as I'm expecting. Let's say I want to calculate
some summation. With the old syntax I could do

@time sum([2*t for t in 1:2:100])
 
0.015104 seconds (13.80 k allocations: 660.366 KB)

that allocates the array within the summation. Now I thought this would a prime example
where the memory overhead could be decreased by using the new notation i.e.

@time sum(2*t for t in 1:2:100)
 
0.019215 seconds (18.98 k allocations: 785.777 KB)

,but generator syntax performs slightly worse. Also if we want find the maximum we would do

julia> @time maximum([2*t for t in 1:2:100])
 
0.015182 seconds (12.90 k allocations: 606.166 KB)
198

julia
> @time maximum(2*t for t in 1:2:100)
 
0.019935 seconds (18.74 k allocations: 772.180 KB)
198

Have I understood the new generator syntax incorrectly or should the new syntax perform
better in these code snippet examples?






Reply | Threaded
Open this post in threaded view
|

Re: 0.5 new generators syntax question

Yichao Yu
On Mon, Oct 31, 2016 at 3:42 PM, Jesse Jaanila <[hidden email]> wrote:

> Hi,
>
> I was experimenting with the new 0.5 features and they are great! But to my
> surprise,
> the generator syntax doesn't work as I'm expecting. Let's say I want to
> calculate
> some summation. With the old syntax I could do
>
> @time sum([2*t for t in 1:2:100])
>   0.015104 seconds (13.80 k allocations: 660.366 KB)
>
> that allocates the array within the summation. Now I thought this would a
> prime example
> where the memory overhead could be decreased by using the new notation i.e.
>
> @time sum(2*t for t in 1:2:100)
>   0.019215 seconds (18.98 k allocations: 785.777 KB)
>
> ,but generator syntax performs slightly worse. Also if we want find the
> maximum we would do
>
> julia> @time maximum([2*t for t in 1:2:100])
>   0.015182 seconds (12.90 k allocations: 606.166 KB)
> 198
>
> julia> @time maximum(2*t for t in 1:2:100)
>   0.019935 seconds (18.74 k allocations: 772.180 KB)
> 198
>
> Have I understood the new generator syntax incorrectly or should the new
> syntax perform
> better in these code snippet examples?

Put it in a function since each syntactically different
generator/anonymous functions has their own type and you are
benchmarking the compiler only. It doesn't make sense for adding 50
numbers to take 15 ms.

>
>
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: 0.5 new generators syntax question

Johan Sigfrids
In reply to this post by Jesse Jaanila
Given that allocating an array for 50 Ints, filling it up, and then summing it all together probably takes less than a microsecond, any difference between allocating and not allocating will disappears in the noise. As Yichao Yu mentions, what you end up measuring is the time it takes to setup the computation. This is a case where using the BenchmarkTools can be really helpful.

julia> using BenchmarkTools

julia> @benchmark sum([2*t for t in 1:2:100])
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     907
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  560.00 bytes
  allocs estimate:  2
  minimum time:     121.00 ns (0.00% GC)
  median time:      131.00 ns (0.00% GC)
  mean time:        162.82 ns (10.91% GC)
  maximum time:     2.45 μs (0.00% GC)

julia> @benchmark sum(2*t for t in 1:2:100)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     962
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  80.00 bytes
  allocs estimate:  3
  minimum time:     86.00 ns (0.00% GC)
  median time:      90.00 ns (0.00% GC)
  mean time:        107.20 ns (6.99% GC)
  maximum time:     3.64 μs (95.05% GC)


On Monday, October 31, 2016 at 9:42:14 PM UTC+2, Jesse Jaanila wrote:
Hi,

I was experimenting with the new 0.5 features and they are great! But to my surprise,
the generator syntax doesn't work as I'm expecting. Let's say I want to calculate
some summation. With the old syntax I could do

@time sum([2*t for t in 1:2:100])
 
0.015104 seconds (13.80 k allocations: 660.366 KB)

that allocates the array within the summation. Now I thought this would a prime example
where the memory overhead could be decreased by using the new notation i.e.

@time sum(2*t for t in 1:2:100)
 
0.019215 seconds (18.98 k allocations: 785.777 KB)

,but generator syntax performs slightly worse. Also if we want find the maximum we would do

julia> @time maximum([2*t for t in 1:2:100])
 
0.015182 seconds (12.90 k allocations: 606.166 KB)
198

julia
> @time maximum(2*t for t in 1:2:100)
 
0.019935 seconds (18.74 k allocations: 772.180 KB)
198

Have I understood the new generator syntax incorrectly or should the new syntax perform
better in these code snippet examples?






Reply | Threaded
Open this post in threaded view
|

Re: 0.5 new generators syntax question

Stefan Karpinski
When in doubt, use @benchmark!

On Tue, Nov 1, 2016 at 5:49 AM, Johan Sigfrids <[hidden email]> wrote:
Given that allocating an array for 50 Ints, filling it up, and then summing it all together probably takes less than a microsecond, any difference between allocating and not allocating will disappears in the noise. As Yichao Yu mentions, what you end up measuring is the time it takes to setup the computation. This is a case where using the BenchmarkTools can be really helpful.

julia> using BenchmarkTools

julia> @benchmark sum([2*t for t in 1:2:100])
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     907
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  560.00 bytes
  allocs estimate:  2
  minimum time:     121.00 ns (0.00% GC)
  median time:      131.00 ns (0.00% GC)
  mean time:        162.82 ns (10.91% GC)
  maximum time:     2.45 μs (0.00% GC)

julia> @benchmark sum(2*t for t in 1:2:100)
BenchmarkTools.Trial:
  samples:          10000
  evals/sample:     962
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  80.00 bytes
  allocs estimate:  3
  minimum time:     86.00 ns (0.00% GC)
  median time:      90.00 ns (0.00% GC)
  mean time:        107.20 ns (6.99% GC)
  maximum time:     3.64 μs (95.05% GC)


On Monday, October 31, 2016 at 9:42:14 PM UTC+2, Jesse Jaanila wrote:
Hi,

I was experimenting with the new 0.5 features and they are great! But to my surprise,
the generator syntax doesn't work as I'm expecting. Let's say I want to calculate
some summation. With the old syntax I could do

@time sum([2*t for t in 1:2:100])
 
0.015104 seconds (13.80 k allocations: 660.366 KB)

that allocates the array within the summation. Now I thought this would a prime example
where the memory overhead could be decreased by using the new notation i.e.

@time sum(2*t for t in 1:2:100)
 
0.019215 seconds (18.98 k allocations: 785.777 KB)

,but generator syntax performs slightly worse. Also if we want find the maximum we would do

julia> @time maximum([2*t for t in 1:2:100])
 
0.015182 seconds (12.90 k allocations: 606.166 KB)
198

julia
> @time maximum(2*t for t in 1:2:100)
 
0.019935 seconds (18.74 k allocations: 772.180 KB)
198

Have I understood the new generator syntax incorrectly or should the new syntax perform
better in these code snippet examples?