# How can I arrange to only use @threads if the number of iteration is higher than minimum?

**URL:** https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177
**Category:** Performance
**Tags:** multithreading
**Created:** [September 14, 2021, 10:34pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177 "2021-09-14T22:34:59Z")
**Posts on this page:** 1
**Showing post:** 18

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [September 16, 2021, 9:33pm UTC](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177/18 "2021-09-16T21:33:33Z")

</div>

> [@Elrod](#):
>
> If it is dominated by threading or scheduling overhead, then Polyester should help.

@Elrod  
I have compared between the `@batch` and the method here:  
[Optional macro invocation](https://discourse.julialang.org/t/optional-macro-invocation/18588/21)

```julia
function ss()
dt = 0.001;     
tmin = 0;  
tmax = 5;
timeSim = tmin:dt:tmax;
A_Mat = spzeros(9,9); 
for i in 1:9
  A_Mat[i,i] = 1;
end
I_Mat = ones(9,3);
  iter = 0;
  for t in timeSim
      iter += 1;
      n=101
      #@threadsif n>100 for i in 1:n #by using it the time is 182 ms
      @batch minbatch=15 for i in 1:n #by using it the time is 879.872 ms
      V_Mat = A_Mat \ I_Mat;
    end
end
end
using SparseArrays;
using LinearAlgebra;
using .Threads
using Polyester
using BenchmarkTools
@btime ss()

```

Any reason why `@batch` results in a really big number comparing with conditional thread `@threadsif `?

---

_[View the full topic](https://discourse.julialang.org/t/how-can-i-arrange-to-only-use-threads-if-the-number-of-iteration-is-higher-than-minimum/68177)._
