# Help speed up the function

**URL:** <https://discourse.julialang.org/t/help-speed-up-the-function/19683>\
**Category:** New to Julia\
**Created:** [January 16, 2019, 4:52am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683 "2019-01-16T04:52:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)\
**Post date:** [January 16, 2019, 4:52am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/1 "2019-01-16T04:52:58Z")

</div>

ArLog - Global array

```julia
ArLog = Array{String,1}()
push!(ArLog, "Drop/BC/1.bin")
push!(ArLog, "Drop/BC/2.bin")
....

```

```julia
searchHash = function(n::BigInt,C::BitArray)
		for line in ArLog
			s1 = open(line,"r+")
			A2 = Mmap.mmap(s1, BitArray, (64,10^9));
			for i in eachindex(A2)
				for k in eachindex(C)
					if(A2[1:64,i] == C[1:64,k] )
						SaveRes(arLine[2],string(i),string(n))
					end
					k == 438 && break
				end
				i == 10^9 && break
			end
			finalize(A2)
			close(s1)
			GC.gc();
		end	
	end

```

Now this function is very slow.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 16, 2019, 5:18am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/2 "2019-01-16T05:18:56Z")

</div>

`const`

---

<div class="post-metadata">

**Author:** ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)\
**Post date:** [January 16, 2019, 5:20am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/3 "2019-01-16T05:20:00Z")

</div>

what?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 16, 2019, 5:20am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/4 "2019-01-16T05:20:52Z")

</div>

`const ArLog`

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 16, 2019, 5:22am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/5 "2019-01-16T05:22:28Z")

</div>

Also note that the way you’re defining functions will make calling them very slow because they are also not const.

---

<div class="post-metadata">

**Author:** ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)\
**Post date:** [January 16, 2019, 5:25am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/6 "2019-01-16T05:25:34Z")

</div>

> [@StefanKarpinski](#):
>
> const

Perhaps this will speed execution, but the bottleneck is clearly here.

```julia
for i in eachindex(A2)
   for k in eachindex(C)
	if(A2[1:64,i] == C[1:64,k] )
		SaveRes(arLine[2],string(i),string(n))
	end
	k == 438 && break
   end
   i == 10^9 && break
end

```

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [January 16, 2019, 5:35am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/7 "2019-01-16T05:35:10Z")

</div>

Since ArLog is not type stable, the type of line can not be inferred along with s1, A2, i, and k. This results in a massive reductions in speed. Your bottleneck will run faster if you make that change.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 16, 2019, 5:54am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/8 "2019-01-16T05:54:35Z")

</div>

Two low-hanging fruits are

1. providing the `ArLog` as an argument to the function,
2. using the standard way of defining functions (instead of defining a closure and assigning it to a variable like you are doing — where did you get that idea?)

Eg

```julia
function search_hash(n::BigInt,C::BitArray, ar_log)
    ...
end

```

Also, you should consider using [standard naming conventions](https://docs.julialang.org/en/v1/manual/style-guide/#Use-naming-conventions-consistent-with-Julia-base/-1), and read the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/).

---

<div class="post-metadata">

**Author:** ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)\
**Post date:** [January 16, 2019, 5:57am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/9 "2019-01-16T05:57:59Z")

</div>

Here is a more detailed code.

```julia
@everywhere struct Point{T}
    x::T
    y::T
end
function bigfunc(ArPub,ArLog)

	searchHash = function(n::BigInt,C::BitArray)
		for line in ArLog
			s1 = open(line,"r+")
			A2 = Mmap.mmap(s1, BitArray, (64,10^9));
			for i in eachindex(A2)
				for k in eachindex(C)
					if(A2[1:64,i] == C[1:64,k] )
						SaveRes(arLine[2],string(i),string(n))
					end
					k == length(ArPub) && break
				end
				i == 10^9 && break
			end
			finalize(A2)
			close(s1)
			GC.gc();
		end	
	end
	GenC = function(n::BigInt)
		C = falses(64,length(ArPub))
		...
		searchHash(n,C)	
		...
	end
	GenC(big(10)^20)
end	

ArPub = Point{BigInt}[]
open("Points.txt") do f
     for line in eachline(f)
		if line[1:2] == "04"
			point = Point{BigInt}(parse(BigInt, line[3:66], base=16), parse(BigInt, line[67:end], base=16))
			push!(ArPub, point)
		end
    end	
end

ArLog = Array{String,1}()
open("Drop/BC/log.txt") do f
     for line in eachline(f)
		arLine = split(line," ")
		push!(ArLog, string(arLine[2]))
    end
end
bigfunc(ArPub,ArLog)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 16, 2019, 11:20am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/10 "2019-01-16T11:20:09Z")

</div>

You haven’t made any of the changes that was suggested?

This is useful reading [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/index.html), many of the problems with the code is described there.

---

<div class="post-metadata">

**Author:** ![1112](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1112/32/9325_2.png) [@1112](https://discourse.julialang.org/u/1112)\
**Post date:** [January 17, 2019, 4:18am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/11 "2019-01-17T04:18:44Z")

</div>

I made all the changes proposed above. This gave a slight increase in performance.  
The bottleneck still the is here.

```julia
for i in eachindex(A2)
	chone = A2[1:64,i]
	for k in eachindex(C)
		if chone == C[1:64,k]
			SaveRes(arLine[2],string(i),string(n))
		end
		k == length(ArPub) && break
	end
	i == 10^9 && break
end

```

I removed from the second cycle “A2 [1: 64, i]” into the variable chone, this gave a noticeable performance boost.  
But still the function works very slowly.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [January 17, 2019, 4:38am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/12 "2019-01-17T04:38:05Z")

</div>

I don’t mean to be rude, but if you’re expecting us to help you speed up your code, you could at least provide a Minimum Working Example that we can use to benchmark on our local machines. Otherwise, all we can do is point out anything obvious that we see in the code you’ve posted, which won’t likely won’t get you nearly the results you desire.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 17, 2019, 6:49am UTC](https://discourse.julialang.org/t/help-speed-up-the-function/19683/13 "2019-01-17T06:49:06Z")

</div>

> [@1112](#):
>
> But still the function works very slowly.

What you have posted is not a function.

Please provide a self-contained MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …
