# \[FAQ\]If I have big big loop, How to reduce the running time

**URL:** https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717
**Category:** General Usage
**Created:** [December 12, 2020, 1:31pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717 "2020-12-12T13:31:45Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ZhouZhuofei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhouzhuofei/32/18399_2.png) [@ZhouZhuofei](https://discourse.julialang.org/u/ZhouZhuofei)
#### Post date: [December 12, 2020, 1:31pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/1 "2020-12-12T13:31:45Z")

</div>

For example:  
if n = One trillion

```nohighlight
for _ in 1:n
   ######
end

```

in python, binary expansion can help, how to do in Julia.  
add more information about:

```nohighlight
import .Base: *, +
struct FieldElement
	Num::BigInt
    Prime::BigInt
	function FieldElement(Num, Prime)
		if Num < 0 || Prime ≤ Num
			return "Num $(Num) not in field range 0 to $(Prime - 1)"
		else
			new(Num, Prime)
		end
    end	
end

function +(A::FieldElement, B::FieldElement)
	if A.Prime != B.Prime
		return "Cannot add two numbers in different Fields"
	else
		res = mod(A.Num + B.Num, B.Prime)
		return FieldElement(res, B.Prime)
	end
end

function *(Num::BigInt, A::FieldElement)
    sum = A
    for _ in 2:Num
        sum += A
    end
    return sum
end

using BenchmarkTools
p = 115792089237316195423570985008687907853269984665640564039457584007908834671663
Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
@btime (BigInt(2)^BigInt(10)) * FieldElement(Gx, p)

```

so like that even big Num, will take more time…

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [December 12, 2020, 2:01pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/2 "2020-12-12T14:01:47Z")

</div>

Ok, lets try your “#####” and a bit more:

```julia
julia> using BenchmarkTools
[Info: Precompiling BenchmarkTools [6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf]
julia> function trillion()
           j=1
           for i in 1:1_000_000_000_000_000_000
              j=i
              #######
           end
       end
trillion (generic function with 1 method)

julia> @btime trillion()
  1.099 ns (0 allocations: 0 bytes)

```

Wow, I knew, Julia is just lightning fast, no need for python anymore or binary expansion.

😉

---

<div class="post-metadata">

### Author: ![ZhouZhuofei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhouzhuofei/32/18399_2.png) [@ZhouZhuofei](https://discourse.julialang.org/u/ZhouZhuofei)
#### Post date: [December 12, 2020, 2:41pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/3 "2020-12-12T14:41:33Z")

</div>

😃Thank you for your advice

but if n =2^{32} even more, It also takes a lot of time. 😂

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [December 12, 2020, 2:52pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/4 "2020-12-12T14:52:42Z")

</div>

So, maybe it’s my extremely overpowered rig:

```julia
julia> function less_than_a_trillion()
       j=1
       for i in 1:(2^32)
       j=i
       ######
       end
       end
trillion (generic function with 1 method)

julia> @btime less_than_a_trillion()
  1.099 ns (0 allocations: 0 bytes)

```

I always wondered why it’s so warm and cosy here… 🙂

Of course, I am joking, it is just that I don’t really understand your question. For me, it seems way too general to be answered in a serious way. But if you like, you could elaborate a bit more on your question?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [December 12, 2020, 3:19pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/5 "2020-12-12T15:19:13Z")

</div>

I am almost sure the compiler is smart enough to replace the loop by just `j = 2^32`, so this test is useless, a rebinding a variable is almost always a no-op. It is impossible to do a serious test unless we know actually what will be done inside the loop.

---

<div class="post-metadata">

### Author: ![ZhouZhuofei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhouzhuofei/32/18399_2.png) [@ZhouZhuofei](https://discourse.julialang.org/u/ZhouZhuofei)
#### Post date: [December 12, 2020, 3:55pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/6 "2020-12-12T15:55:19Z")

</div>

I add some details for my question.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [December 12, 2020, 4:16pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/7 "2020-12-12T16:16:52Z")

</div>

> [@ZhouZhuofei](#):
>
> ```julia
> sum = A
> for _ in 2:Num
> sum += A
> end
> 
> ```

Isn’t this equal to:

```julia
Num * A.Num

```

like

```julia
function *(Num::BigInt, A::FieldElement)
    return FieldElement( Num * A.Num, A.Prime )
end

```

?

---

<div class="post-metadata">

### Author: ![ZhouZhuofei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhouzhuofei/32/18399_2.png) [@ZhouZhuofei](https://discourse.julialang.org/u/ZhouZhuofei)
#### Post date: [December 12, 2020, 5:13pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/9 "2020-12-12T17:13:02Z")

</div>

> [@oheil](#):
>
> `return FieldElement( Num * A.Num, A.Prime )`

not equal,

```Julia
function *(Num::BigInt, A::FieldElement)
	return FieldElement(mod(Num * A.Num, A.Prime), A.Prime)
end

```

I’m sorry, But I didn’t notice that FieldElement could do this. 😄

But mostly I want to know about special multiplications (which can only be done by adding all the time, e.g., addition on elliptic curves).

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [December 12, 2020, 5:33pm UTC](https://discourse.julialang.org/t/faq-if-i-have-big-big-loop-how-to-reduce-the-running-time/51717/10 "2020-12-12T17:33:17Z")

</div>

Yes, sorry, missed the `mod`.
