# Performance

**URL:** https://discourse.julialang.org/c/usage/perf/37.md?page=52

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

**Page:** 53

---

## [Fastest way to add arrays](https://discourse.julialang.org/t/fastest-way-to-add-arrays/91593)

<div class="topic-metadata">

**Author:** [@Marcell\_Havlik](https://discourse.julialang.org/u/Marcell_Havlik)\
**Replies:** 12\
**Last updated:** [December 14, 2022, 11:37am UTC](https://discourse.julialang.org/t/fastest-way-to-add-arrays/91593 "2022-12-14T11:37:54Z")

</div>

Hey Julianners, Can you help me on how to create fast array addition? I believe this is the fastest way: using CUDA using BenchmarkTools BenchmarkTools.DEFAULT\_PARAMETERS.seconds = 1.00 add\_cab(c, a, b) = @inbounds …

---

## [Why \`@nospecialize(ex)\` in macros?](https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589)

<div class="topic-metadata">

**Author:** [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Replies:** 2\
**Last updated:** [December 14, 2022, 8:28am UTC](https://discourse.julialang.org/t/why-nospecialize-ex-in-macros/91589 "2022-12-14T08:28:04Z")

</div>

Why do Julia experts use @nospecialize(ex) in functions used by macros? E.g. here: Typed globals: macro to define multiple, without manual type annotations - #2 by simeonschaub I’d say the only types the argument could…

---

## [There is a huge difference in time consumption between calling a function sentence by sentence and calling the entire function at once](https://discourse.julialang.org/t/there-is-a-huge-difference-in-time-consumption-between-calling-a-function-sentence-by-sentence-and-calling-the-entire-function-at-once/82946)

<div class="topic-metadata">

**Author:** [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Replies:** 33\
**Last updated:** [December 13, 2022, 8:50am UTC](https://discourse.julialang.org/t/there-is-a-huge-difference-in-time-consumption-between-calling-a-function-sentence-by-sentence-and-calling-the-entire-function-at-once/82946 "2022-12-13T08:50:01Z")

</div>

First, I statistics time consumer of each sentence with @btime: function (m::CoarseDecoder)(bg::T, fg, mask::T, encoder\_outs=nothing) where{T} bg\_x = bg mask\_x = mask mask\_outs = Vector{T}() bg\_outs = Ve…

---

## [Inplace multiplication of sub-matrices without allocations](https://discourse.julialang.org/t/inplace-multiplication-of-sub-matrices-without-allocations/91534)

<div class="topic-metadata">

**Author:** [@CeterisPartybus](https://discourse.julialang.org/u/CeterisPartybus)\
**Replies:** 12\
**Last updated:** [December 12, 2022, 8:44pm UTC](https://discourse.julialang.org/t/inplace-multiplication-of-sub-matrices-without-allocations/91534 "2022-12-12T20:44:36Z")

</div>

I am writing a performance-critical part and would like to avoid any memory allocations in that part by creating caches beforehand. I then essentially need to multiply sub-arrays in a loop. Is there any way to do these m…

---

## [Why is a multi-argument inplace map much faster in this case than a broadcast?](https://discourse.julialang.org/t/why-is-a-multi-argument-inplace-map-much-faster-in-this-case-than-a-broadcast/91525)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 16\
**Last updated:** [December 12, 2022, 7:14pm UTC](https://discourse.julialang.org/t/why-is-a-multi-argument-inplace-map-much-faster-in-this-case-than-a-broadcast/91525 "2022-12-12T19:14:55Z")

</div>

julia\> A = rand(10, 1000); B = copy(A); C = zero(A); D = zero(A); julia\> @btime map!(+, $C, $A, $B); 3.405 μs (0 allocations: 0 bytes) julia\> @btime $D .= $A .+ $B; 10.915 μs (0 allocations: 0 bytes) julia\> A = r…

---

## [@btime with local function --\> not defined](https://discourse.julialang.org/t/btime-with-local-function-not-defined/10204)

<div class="topic-metadata">

**Author:** [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Replies:** 3\
**Last updated:** [December 12, 2022, 4:06pm UTC](https://discourse.julialang.org/t/btime-with-local-function-not-defined/10204 "2022-12-12T16:06:13Z")

</div>

dear J wizards—how do I use @btime inside a function with a passed argument? julia\> function testme( afun ); @btime begin afun(2) end end testme (generic function with 1 method) julia\> testme( println ) ERROR: UndefVar…

---

## [Can threads communicate with each other while in multithreading?](https://discourse.julialang.org/t/can-threads-communicate-with-each-other-while-in-multithreading/91550)

<div class="topic-metadata">

**Author:** [@le\_ackerman](https://discourse.julialang.org/u/le_ackerman)\
**Replies:** 2\
**Last updated:** [December 12, 2022, 1:07pm UTC](https://discourse.julialang.org/t/can-threads-communicate-with-each-other-while-in-multithreading/91550 "2022-12-12T13:07:28Z")

</div>

Hi! I am a beginner in parallel computing and been fooling around with different packages in multithreading. My question is very specific: I want to write a code to do multithreading… and i want to know is there any …

---

## [Using LinearMap with MPI.jl](https://discourse.julialang.org/t/using-linearmap-with-mpi-jl/91464)

<div class="topic-metadata">

**Author:** [@devanshu](https://discourse.julialang.org/u/devanshu)\
**Replies:** 5\
**Last updated:** [December 11, 2022, 6:34am UTC](https://discourse.julialang.org/t/using-linearmap-with-mpi-jl/91464 "2022-12-11T06:34:24Z")

</div>

Hi, I am finding difficulty in using LinearMaps while also trying to parallelize the code using MPI.jl. Actually, I am using ArnoldiMethod.jl in order to implement shift-invert diagonalization technique. The minimal co…

---

## [Simple CRPS optimization](https://discourse.julialang.org/t/simple-crps-optimization/91473)

<div class="topic-metadata">

**Author:** [@cmarcotte](https://discourse.julialang.org/u/cmarcotte)\
**Replies:** 1\
**Last updated:** [December 9, 2022, 1:53pm UTC](https://discourse.julialang.org/t/simple-crps-optimization/91473 "2022-12-09T13:53:06Z")

</div>

I have a short function for computing the Continuous Ranked Probability Score (CRPS) from an ensemble of states Xb::Array{Float64,2} and a set comparison state y::Array{Float64,1}, where y may have varying sparsity (i.e.…

---

## [Reduce compilation time for long and nested tuples](https://discourse.julialang.org/t/reduce-compilation-time-for-long-and-nested-tuples/91392)

<div class="topic-metadata">

**Author:** [@osimonn](https://discourse.julialang.org/u/osimonn)\
**Replies:** 4\
**Last updated:** [December 9, 2022, 2:45am UTC](https://discourse.julialang.org/t/reduce-compilation-time-for-long-and-nested-tuples/91392 "2022-12-09T02:45:24Z")

</div>

Details: I am calling C functions and using C structures from Julia by using the Clang.jl to automate the wrapping process. In the C code, there are multiple arrays of one or more dimensions. In order to have a one-to-o…

---

## [How to easily troubleshoot type instability traps in complex code?](https://discourse.julialang.org/t/how-to-easily-troubleshoot-type-instability-traps-in-complex-code/91439)

<div class="topic-metadata">

**Author:** [@tiZ](https://discourse.julialang.org/u/tiZ)\
**Replies:** 1\
**Last updated:** [December 8, 2022, 6:45pm UTC](https://discourse.julialang.org/t/how-to-easily-troubleshoot-type-instability-traps-in-complex-code/91439 "2022-12-08T18:45:16Z")

</div>

I know that type stability is very important in Julia, so how can I get a warning when there is a type change in my code? For example, I now have a very long code function f(x) that runs slowly, and I want to find out i…

---

## [Is back propagation in Flux alway run on a single cpu core?](https://discourse.julialang.org/t/is-back-propagation-in-flux-alway-run-on-a-single-cpu-core/91379)

<div class="topic-metadata">

**Author:** [@tiZ](https://discourse.julialang.org/u/tiZ)\
**Replies:** 0\
**Last updated:** [December 7, 2022, 6:07pm UTC](https://discourse.julialang.org/t/is-back-propagation-in-flux-alway-run-on-a-single-cpu-core/91379 "2022-12-07T18:07:08Z")

</div>

Hi, I am trainning a neural network with Flux on a machine with 56 cpu cores, but I found that it seems it is alway only one core working while Flux.train! is performed, and I can see multi cpu is running in the forwar…

---

## [Type system debug bool to elide code based on function parameter](https://discourse.julialang.org/t/type-system-debug-bool-to-elide-code-based-on-function-parameter/91378)

<div class="topic-metadata">

**Author:** [@taotree](https://discourse.julialang.org/u/taotree)\
**Replies:** 0\
**Last updated:** [December 7, 2022, 6:06pm UTC](https://discourse.julialang.org/t/type-system-debug-bool-to-elide-code-based-on-function-parameter/91378 "2022-12-07T18:06:46Z")

</div>

I’m sure there is a known idiom for this, but I can’t find it at the moment. I want to write something like: function run(a, b, debugBool) # ... do lots of stuff debugBool && @show debug info # ... do lots of stu…

---

## [Array of union causing 10x slowdown](https://discourse.julialang.org/t/array-of-union-causing-10x-slowdown/91341)

<div class="topic-metadata">

**Author:** [@Ininterrompue](https://discourse.julialang.org/u/Ininterrompue)\
**Replies:** 6\
**Last updated:** [December 7, 2022, 5:35pm UTC](https://discourse.julialang.org/t/array-of-union-causing-10x-slowdown/91341 "2022-12-07T17:35:39Z")

</div>

I have a type-stability question. In my 2D axisymmetric ray tracing code, I define an array of surfaces that has the type Union{Cone, Plane}, like surfaces = Matrix{Union{Cone, Plane}}(undef, 100, 2) All of them should…

---

## [Dict doesn't reuse deleted slots?](https://discourse.julialang.org/t/dict-doesnt-reuse-deleted-slots/45335)

<div class="topic-metadata">

**Author:** [@Christian\_Rorvik](https://discourse.julialang.org/u/Christian_Rorvik)\
**Replies:** 1\
**Last updated:** [December 7, 2022, 3:52pm UTC](https://discourse.julialang.org/t/dict-doesnt-reuse-deleted-slots/45335 "2022-12-07T15:52:16Z")

</div>

I’ve come upon a performance gotcha with Dict, wherein large amounts of insertions and deletions cause rehashing and allocations even when the load factor is stable. I don’t know much about hash table implementations, s…

---

## [Running a process on several nodes on cluster](https://discourse.julialang.org/t/running-a-process-on-several-nodes-on-cluster/91203)

<div class="topic-metadata">

**Author:** [@Rick](https://discourse.julialang.org/u/Rick)\
**Replies:** 12\
**Last updated:** [December 6, 2022, 3:56am UTC](https://discourse.julialang.org/t/running-a-process-on-several-nodes-on-cluster/91203 "2022-12-06T03:56:42Z")

</div>

Hi everyone, I am trying to run a process that requires a huge amount of memory on several nodes on cluster. I am trying to use the Distributed.jl package, but it does not seem to work, so I would like to seek some advi…

---

## [How To Avoiding Allocations in Static Structs](https://discourse.julialang.org/t/how-to-avoiding-allocations-in-static-structs/91196)

<div class="topic-metadata">

**Author:** [@cdelv](https://discourse.julialang.org/u/cdelv)\
**Replies:** 4\
**Last updated:** [December 5, 2022, 4:48pm UTC](https://discourse.julialang.org/t/how-to-avoiding-allocations-in-static-structs/91196 "2022-12-05T16:48:08Z")

</div>

Hi, I’m doing an MD simulation and have a problem getting good performance with static structs. The idea is to have a struct that represents a particle. I checked that using static structs is the fastest (nice) way of do…

---

## [N-dimensional ConvLayer by scratch with all the bells and whistles but... super super slow :-/](https://discourse.julialang.org/t/n-dimensional-convlayer-by-scratch-with-all-the-bells-and-whistles-but-super-super-slow/91104)

<div class="topic-metadata">

**Author:** [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Replies:** 5\
**Last updated:** [December 5, 2022, 4:30pm UTC](https://discourse.julialang.org/t/n-dimensional-convlayer-by-scratch-with-all-the-bells-and-whistles-but-super-super-slow/91104 "2022-12-05T16:30:59Z")

</div>

I just implemented a fully-from-scratch convolutional layer on my BetaML ML lib. It works with or without autodiff, it accepts infinite dimensions, nchannels input/output, padding, stride… training even works …

---

## [Performant and user-friendly function only through metaprogramming?](https://discourse.julialang.org/t/performant-and-user-friendly-function-only-through-metaprogramming/91268)

<div class="topic-metadata">

**Author:** [@corbat](https://discourse.julialang.org/u/corbat)\
**Replies:** 1\
**Last updated:** [December 5, 2022, 3:49pm UTC](https://discourse.julialang.org/t/performant-and-user-friendly-function-only-through-metaprogramming/91268 "2022-12-05T15:49:11Z")

</div>

Hi, I have a question on how to write performant functions that are still somewhat convenient for the user. I have an application where I need to run optimizers on custom functions, i.e., the functions are called very o…

---

## [@spawn and @async in producer-consumer problem](https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252)

<div class="topic-metadata">

**Author:** [@achjaj](https://discourse.julialang.org/u/achjaj)\
**Replies:** 4\
**Last updated:** [December 5, 2022, 2:09pm UTC](https://discourse.julialang.org/t/spawn-and-async-in-producer-consumer-problem/91252 "2022-12-05T14:09:37Z")

</div>

Hello, I am trying to implement a producer-consumer problem, where I want one thread to produce some data and a second thread to take the data and write them to a disk. I am using Channel as my queue so that the produce…

---

## [Optimize code by parallelization/GPU](https://discourse.julialang.org/t/optimize-code-by-parallelization-gpu/88578)

<div class="topic-metadata">

**Author:** [@Adegasel](https://discourse.julialang.org/u/Adegasel)\
**Replies:** 8\
**Last updated:** [October 12, 2022, 8:46pm UTC](https://discourse.julialang.org/t/optimize-code-by-parallelization-gpu/88578 "2022-10-12T20:46:14Z")

</div>

I have developed this transform method in order to assign vectors to previously computed centroids. It is currently taking ~20s on my machine, for 1M vectors and 256 clusters. Since I want to apply it to 1B vectors, this…

---

## [Generating a sparse matrix from a piecewise function](https://discourse.julialang.org/t/generating-a-sparse-matrix-from-a-piecewise-function/91119)

<div class="topic-metadata">

**Author:** [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)\
**Replies:** 2\
**Last updated:** [December 2, 2022, 12:36am UTC](https://discourse.julialang.org/t/generating-a-sparse-matrix-from-a-piecewise-function/91119 "2022-12-02T00:36:46Z")

</div>

I’m trying to generate a random sparse matrix R where each element in R can take on the following values: r\_{ij}= \\left\\{ \\begin{array}{ll} 1 & \\text{with probability } \\frac{1}{6}\\\\ 0 & \\text{with probab…

---

## [In julia import Pkg failed to precompiling. Why and how to solve this problem?](https://discourse.julialang.org/t/in-julia-import-pkg-failed-to-precompiling-why-and-how-to-solve-this-problem/91108)

<div class="topic-metadata">

**Author:** [@connect2robiul](https://discourse.julialang.org/u/connect2robiul)\
**Replies:** 1\
**Last updated:** [December 1, 2022, 5:31pm UTC](https://discourse.julialang.org/t/in-julia-import-pkg-failed-to-precompiling-why-and-how-to-solve-this-problem/91108 "2022-12-01T17:31:21Z")

</div>

---

## [Julia pmap how to write each worker into separate index using parallel computing](https://discourse.julialang.org/t/julia-pmap-how-to-write-each-worker-into-separate-index-using-parallel-computing/90961)

<div class="topic-metadata">

**Author:** [@Pedro\_Raposo](https://discourse.julialang.org/u/Pedro_Raposo)\
**Replies:** 16\
**Last updated:** [November 30, 2022, 9:07pm UTC](https://discourse.julialang.org/t/julia-pmap-how-to-write-each-worker-into-separate-index-using-parallel-computing/90961 "2022-11-30T21:07:29Z")

</div>

I have a function that runs a simulation exercise. I call the function “raposo” and it has an output that is a vector (1,9). I need to compute it using parallel computing because it is taking 3 months to produce the outp…

---

## [The built-in complex division is unreasonably slow](https://discourse.julialang.org/t/the-built-in-complex-division-is-unreasonably-slow/91026)

<div class="topic-metadata">

**Author:** [@photor](https://discourse.julialang.org/u/photor)\
**Replies:** 3\
**Last updated:** [November 30, 2022, 5:13pm UTC](https://discourse.julialang.org/t/the-built-in-complex-division-is-unreasonably-slow/91026 "2022-11-30T17:13:29Z")

</div>

@btime x/y setup=(x=rand(ComplexF64);y=rand(ComplexF64)); gives 9.853 ns (0 allocations: 0 bytes) while function frac(x::ComplexF64, y::ComplexF64) y2=y.re^2+y.im^2 return Complex((x.re\*y.re+x.im\*y.im)/y2, (x…

---

## [Dividing array by float: is multiplying with precalculated inverse worth it](https://discourse.julialang.org/t/dividing-array-by-float-is-multiplying-with-precalculated-inverse-worth-it/90943)

<div class="topic-metadata">

**Author:** [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Replies:** 12\
**Last updated:** [November 30, 2022, 4:11pm UTC](https://discourse.julialang.org/t/dividing-array-by-float-is-multiplying-with-precalculated-inverse-worth-it/90943 "2022-11-30T16:11:17Z")

</div>

What is best, bigvec ./ some\_float() or bigvec .\* (1 / some\_float()) ? I looked at @code\_native to see if the first gets compiled to the second, but — from the little I understand of the output — it doesn’t seem so? …

---

## [Slow deletion loop on huge DataFrame](https://discourse.julialang.org/t/slow-deletion-loop-on-huge-dataframe/90970)

<div class="topic-metadata">

**Author:** [@Kryppi](https://discourse.julialang.org/u/Kryppi)\
**Replies:** 2\
**Last updated:** [November 29, 2022, 9:49pm UTC](https://discourse.julialang.org/t/slow-deletion-loop-on-huge-dataframe/90970 "2022-11-29T21:49:59Z")

</div>

I want to delete certain elements in an ordered DataFrame dim=(1.5 million,14). If the conditions for deletion are met, the code to check if the next element is also up for deletion (if the string in \[x,1\] is identical) …

---

## [How to get average salary for each experience level for each job title?](https://discourse.julialang.org/t/how-to-get-average-salary-for-each-experience-level-for-each-job-title/91003)

<div class="topic-metadata">

**Author:** [@akshdfyehd](https://discourse.julialang.org/u/akshdfyehd)\
**Replies:** 1\
**Last updated:** [November 29, 2022, 5:52pm UTC](https://discourse.julialang.org/t/how-to-get-average-salary-for-each-experience-level-for-each-job-title/91003 "2022-11-29T17:52:57Z")

</div>

Hi, I got the dataset like following: I would like to get average salary for each experience level in each job title, I have tried: second=combine(groupby(new,:experience\_level,),:salary\_in\_usd =\>IMD.mean) but the…

---

## [ODE solver with too many function calls](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686)

<div class="topic-metadata">

**Author:** [@c\_sell](https://discourse.julialang.org/u/c_sell)\
**Replies:** 23\
**Last updated:** [November 28, 2022, 7:48pm UTC](https://discourse.julialang.org/t/ode-solver-with-too-many-function-calls/90686 "2022-11-28T19:48:40Z")

</div>

Hello, I am using an Ode solver to simulate a mechanical system with a mass and a spring. Besides the spring and an external force there is a nonlinear force acting on the system. Each computation takes around 4 ms. Wh…

---

## [Why is copying using a loop is much slower than \`copy\` for large arrays?](https://discourse.julialang.org/t/why-is-copying-using-a-loop-is-much-slower-than-copy-for-large-arrays/90633)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 10\
**Last updated:** [November 27, 2022, 3:25am UTC](https://discourse.julialang.org/t/why-is-copying-using-a-loop-is-much-slower-than-copy-for-large-arrays/90633 "2022-11-27T03:25:28Z")

</div>

julia\> function copy2(A::Array) B = similar(A); @inbounds @simd for i in eachindex(A, B) B\[i\] = A\[i\] end B end copy2 (generic function with 1 method) jul…

[Previous page](https://discourse.julialang.org/c/usage/perf/37.md?page=51)

[Next page](https://discourse.julialang.org/c/usage/perf/37.md?page=53)
