-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuntyped-variables.jl
62 lines (55 loc) · 1.17 KB
/
untyped-variables.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using BenchmarkTools
global x = rand(1000);
function sum_global()
s = 0.0
for i in x
s += i
end
return s
end;
function sum_global_annotated()
s = 0.0
for i in x::Vector{Float64}
s += i
end
return s
end;
function sum_local(x)
s = 0.0
for i in x
s += i
end
return s
end;
function sum_local_annotated(x)
s = 0.0
for i in x::Vector{Float64}
s += i
end
return s
end;
println()
println("-----------BENCHMARK-----------")
println()
println("Sum over a vector")
print("global vector: "); @btime sum_global();
print("global vector annotated:"); @btime sum_global_annotated();
print("local vector: "); @btime sum_local(x);
print("local vector annotated: "); @btime sum_local_annotated(x);
x = rand(10_000);
function mutateglobal()
for i in eachindex(x::Vector)
x[i] = rand()
end
end
function mutatelocal(x)
for i in eachindex(x::Vector)
x[i] = rand()
end
end
println()
println("-----------BENCHMARK-----------")
println()
println("Mutating a vector")
print("global mutation:"); @btime mutateglobal();
print("local mutation: "); @btime mutatelocal(x);