# Generator expressions

**URL:** <https://discourse.julialang.org/t/generator-expressions/110337>\
**Category:** New to Julia\
**Tags:** memory-allocation, generator\
**Created:** [February 17, 2024, 10:55am UTC](https://discourse.julialang.org/t/generator-expressions/110337 "2024-02-17T10:55:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)\
**Post date:** [February 17, 2024, 10:55am UTC](https://discourse.julialang.org/t/generator-expressions/110337/1 "2024-02-17T10:55:49Z")

</div>

I’m reading the Julia Manual on Generator Expressions.

According to the Manual … the following expression sums a series without allocating memory:

> sum(1/n^2 for n in 1:1000)

So I tried to compare the previous expression with this other one, which, allegedly, consumes memory (it contains a list comprehension):

> sum([1/n^2 for n in 1:1000])

Well, the result is exactly the opposite: have a look at what @time returns:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/b/1bd852db237d9b49fe10f1887c24a74dd1aa836c.png)

It seems to me that generator Expressions consume more than twice as much time and memory space than their List comprehensions counterparts.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [February 17, 2024, 11:04am UTC](https://discourse.julialang.org/t/generator-expressions/110337/2 "2024-02-17T11:04:44Z")

</div>

I recommend using `@btime` from `BenchmarkTools`, which removes initial compilation from the benchmark and repeats the benmark multiple times. Here are the results I obtain with `@btime`:

```julia
julia> @btime sum(1/n^2 for n in 1:1000)
  3.178 μs (0 allocations: 0 bytes)
1.6439345666815615

julia> @btime sum([1/n^2 for n in 1:1000])
  1.806 μs (1 allocation: 7.94 KiB)
1.6439345666815612

```

I’m not sure why there is a time difference, but the memory problem is resolved.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [February 17, 2024, 11:11am UTC](https://discourse.julialang.org/t/generator-expressions/110337/3 "2024-02-17T11:11:44Z")

</div>

If you look at the output of the timing, you see “99.63% compilation time”, which tells you that you are not measuring runtime.

---

<div class="post-metadata">

**Author:** ![putianyi888](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/putianyi888/32/32279_2.png) [@putianyi888](https://discourse.julialang.org/u/putianyi888)\
**Post date:** [February 19, 2024, 7:46pm UTC](https://discourse.julialang.org/t/generator-expressions/110337/4 "2024-02-19T19:46:13Z")

</div>

notice that they have different results, so maybe they use different strategies

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 20, 2024, 11:26am UTC](https://discourse.julialang.org/t/generator-expressions/110337/5 "2024-02-20T11:26:08Z")

</div>

The difference in execution times is as expected for much larger ranges.  
Probably for “narrow” ranges the overload of the generator function takes over (!?).  
The sum method works very well with a function at the first argument.

```julia
julia> @btime sum([1/n^2 for n in 1:10^6])
  1.720 ms (2 allocations: 7.63 MiB)
1.6449330668487272

julia> @btime sum(1/n^2 for n in 1:10^6)
  853.000 μs (0 allocations: 0 bytes)
1.64493306684877

julia> @btime sum(n->1/n^2,1:10^6)
  445.200 μs (0 allocations: 0 bytes)
1.6449330668487272

```

And better yet it would do the following function 😄

```julia
sumoneovern2(oneto=10^6)= oneto>10 ? 9.869604401089358/6 : sum([1/n^2 for n in 1:10])

ulia> @btime sumoneovern2(10^5)
  0.800 ns (0 allocations: 0 bytes)
1.6449340668482264

```
