here is an attempt :
using Base.Threads
using Base.Iterators
using BenchmarkTools
# include("./secp256k1.jl")
function gen_keys_range(k_range, out_matrix=nothing)
if isnothing(out_matrix)
data_size = length(k_range)
keys = zeros(BigInt, data_size, 2)
else
keys = out_matrix
end
for (i, k) in enumerate(k_range)
x,y = big(1), big(1)
keys[i,1] = x
keys[i,2] = y
end
return keys, k_range
end
function gen_keys_1(k_start, k_num)
nth = Threads.nthreads()
keys = zeros(BigInt, k_num, 2)
k_end = k_start + k_num - 1
part_size = div(k_num, nth)
tasks = [Threads.@spawn(gen_keys_range(k_r)) for k_r in partition(k_start:k_end, part_size)]
results = fetch.(tasks)
for res in results
k, k_r = res
v_start = k_r.start - k_start + 1
v_stop = k_r.stop - k_start + 1
@views keys[v_start:v_stop,:] = k
end
return keys
end
nth = Threads.nthreads()
println("Threads num $nth")
function test_gen_keys_range()
gen_keys_1(123,10000)
end
@btime test_gen_keys_range()
""
giving
> julia -t 1 --project main.jl
Threads num 1
655.200 μs (40045 allocations: 1.07 MiB)
> julia -t auto --project main.jl
Threads num 20
256.800 μs (40649 allocations: 1.09 MiB)
I don’t have your file so I just made two bigint for the test, results are stable after 4 threads because of memory