# What is the difference between map(f, v) and \[f(x) for x in v\]?

**URL:** <https://discourse.julialang.org/t/what-is-the-difference-between-map-f-v-and-f-x-for-x-in-v/84580>\
**Category:** General Usage\
**Created:** [July 21, 2022, 7:37am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-map-f-v-and-f-x-for-x-in-v/84580 "2022-07-21T07:37:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dodoplus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dodoplus/32/30148_2.png) [@dodoplus](https://discourse.julialang.org/u/dodoplus)\
**Post date:** [July 21, 2022, 7:37am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-map-f-v-and-f-x-for-x-in-v/84580/1 "2022-07-21T07:37:47Z")

</div>

Hi,

Take a look at the following code:

```julia
using BenchmarkTools
s = ["$n" for n in 1:10000];
@btime map(x->parse(Int, x), s);
# 174.666 μs (3 allocations: 78.19 KiB)
@btime [x->parse(Int, x) for x in s];
# 3.401 μs (2 allocations: 64 bytes)

```

why is there such a huge difference?

I assumed these two implementations were practically interchangeable.’

Thanks,  
DD

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [July 21, 2022, 7:57am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-map-f-v-and-f-x-for-x-in-v/84580/2 "2022-07-21T07:57:41Z")

</div>

The second statement produces a vector of _functions_, not the values of the function `x->parse(Int, x)` applied to the elements of `s`!

```julia
julia> [x->parse(Int, x) for x in s]
10000-element Vector{var"#57#59"}:
 #57 (generic function with 1 method)
 #57 (generic function with 1 method)
 #57 (generic function with 1 method)
[...]

```

Compare

```julia
julia> @btime [parse(Int, x) for x in $s]
  329.400 μs (3 allocations: 78.19 KiB)

julia> @btime map(x->parse(Int, x), $s);
  321.900 μs (2 allocations: 78.17 KiB)

```

---

<div class="post-metadata">

**Author:** ![dodoplus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dodoplus/32/30148_2.png) [@dodoplus](https://discourse.julialang.org/u/dodoplus)\
**Post date:** [July 21, 2022, 7:59am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-map-f-v-and-f-x-for-x-in-v/84580/3 "2022-07-21T07:59:25Z")

</div>

Oops, you are obviously right.  
Should have been:

```julia
@btime [parse(Int, x) for x in s];
# 174.500 μs (3 allocations: 78.19 KiB)

```

which _is_ exactly the same
