# Transducers vs Iterators

**URL:** <https://discourse.julialang.org/t/transducers-vs-iterators/98771>\
**Category:** Performance\
**Tags:** iterators, transducers\
**Created:** [May 12, 2023, 11:03pm UTC](https://discourse.julialang.org/t/transducers-vs-iterators/98771 "2023-05-12T23:03:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jekyllstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jekyllstein/32/4157_2.png) [@jekyllstein](https://discourse.julialang.org/u/jekyllstein)\
**Post date:** [May 12, 2023, 11:03pm UTC](https://discourse.julialang.org/t/transducers-vs-iterators/98771/1 "2023-05-12T23:03:42Z")

</div>

I have an example of a simple processing pipeline where I noticed Transducers.jl was making a lot of allocations and behaving slowly compared to compositing iterators and generators together. I’m wondering if there’s a way to do this with Transducers.jl that is better.

### Version with iterators and function composition

```julia
function process(input)
       output = Iterators.map(a -> a^2, Iterators.filter(isodd, input))
       Iterators.drop(output, 100) |> first
end

```

```shell
julia> process(a^3 for a in Iterators.countfrom(1))
65944160601201

julia> @btime process(a^3 for a in Iterators.countfrom(1))
  109.819 ns (0 allocations: 0 bytes)
65944160601201

```

### Version with transducers

```julia
function processxf(input)
       input |> Filter(isodd) |> Map(a -> a^2) |> Drop(100) |> first
end

```

```shell
julia> processxf(a^3 for a in Iterators.countfrom(1))
65944160601201
julia> @btime processxf(a^3 for a in Iterators.countfrom(1))
  1.149 μs (109 allocations: 3.50 KiB)
65944160601201

```

I tried to check with Cthulhu for type instability or some other obvious problem but couldn’t find anything.
