# Atomic Operations are too expensive.

**URL:** <https://discourse.julialang.org/t/atomic-operations-are-too-expensive/11827>\
**Category:** Julia at Scale\
**Tags:** performance, multithreading\
**Created:** [June 20, 2018, 3:38pm UTC](https://discourse.julialang.org/t/atomic-operations-are-too-expensive/11827 "2018-06-20T15:38:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SohamTamb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sohamtamb/32/4479_2.png) [@SohamTamb](https://discourse.julialang.org/u/SohamTamb)\
**Post date:** [June 20, 2018, 3:38pm UTC](https://discourse.julialang.org/t/atomic-operations-are-too-expensive/11827/1 "2018-06-20T15:38:08Z")

</div>

Are atomic operations supposed to be this costly?  
Is memory and allocations required for simple read/write operations?

```julia
julia> a = Atomic{Int64}(0)
Atomic{Int64}(0)

julia> @btime a[]
  100.546 ns (1 allocation: 16 bytes)
0

julia> @btime a[] = 100
  101.535 ns (1 allocation: 16 bytes)
100

julia> a = 0
0

julia> typeof(a)
Int64

julia> @btime a
  2.335 ns (0 allocations: 0 bytes)
0

julia> @btime a = 100
  0.025 ns (0 allocations: 0 bytes)
100

```

The memory useage when looping on a single array with 1000 integers sometimes goes upto  
2 GB since an operation (not variable) requires memory.

Is it possible to be an error with the benchmarking tools?  
Are any improvements expected in the near future?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [June 20, 2018, 4:01pm UTC](https://discourse.julialang.org/t/atomic-operations-are-too-expensive/11827/2 "2018-06-20T16:01:11Z")

</div>

No. They are not supposed to be this costly. And they are indeed cheaper and non-allocating on my 0.6.3, but really slow on my 0.7.0-alpha.136. So I’d guess this is yet another transient performance bug on 0.7

edit: Issue is opened [https://github.com/JuliaLang/julia/issues/27694](https://github.com/JuliaLang/julia/issues/27694).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 20, 2018, 4:50pm UTC](https://discourse.julialang.org/t/atomic-operations-are-too-expensive/11827/3 "2018-06-20T16:50:54Z")

</div>

Yes they are supposed to be more expensive but no they are not supposed to allocate and no you are not benchmarking correctly since a is a non const global.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 20, 2018, 4:53pm UTC](https://discourse.julialang.org/t/atomic-operations-are-too-expensive/11827/4 "2018-06-20T16:53:44Z")

</div>

And see the issue linked above for the correct way to use the macro.
