-
Notifications
You must be signed in to change notification settings - Fork 1
/
subarray.jl
87 lines (77 loc) · 2.32 KB
/
subarray.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/julia -f
using Base: unsafe_getindex, checkbounds
macro time_func(f, args...)
args = eval(current_module(), Expr(:tuple, args...))::Tuple
argnames = Symbol[gensym() for i in 1:length(args)]
types = map(typeof, args)
quote
function wrapper($(argnames...))
$(Expr(:meta, :noinline))
$f($(argnames...))
end
function timing_wrapper()
println($f, $types)
wrapper($(args...))
gc()
@time for i in 1:10000000
wrapper($(args...))
end
# Profile.print()
gc()
end
timing_wrapper()
end
end
@inline function checkbounds2{T<:Union(Real,AbstractArray,Colon)}(A::AbstractArray, I::Tuple{Vararg{T}})
Base.@_inline_meta
n = length(I)
if n > 0
for dim = 1:(n-1)
(Base._checkbounds(size(A,dim), I[dim]) ||
Base.throw_boundserror(A, I))
end
(Base._checkbounds(Base.trailingsize(A,n), I[n]) ||
Base.throw_boundserror(A, I))
end
end
@inline function getindex2(V::SubArray, I::Int...)
checkbounds2(V, I)
unsafe_getindex(V, I...)
end
@inline function checkbounds3{T<:Union(Real,AbstractArray,Colon)}(A::AbstractArray, I::T...)
n = length(I)
if n > 0
for dim = 1:(n-1)
(Base._checkbounds(size(A,dim), I[dim]) ||
Base.throw_boundserror(A, I))
end
(Base._checkbounds(Base.trailingsize(A,n), I[n]) ||
Base.throw_boundserror(A, I))
end
end
@inline function getindex3(V::SubArray, I::Int...)
checkbounds3(V, I...)
unsafe_getindex(V, I...)
end
@inline function getindex4(V::SubArray, I::Int...)
n = length(I)
if n > 0
for dim = 1:(n - 1)
(Base._checkbounds(size(V, dim), I[dim]) ||
Base.throw_boundserror(V, I))
end
(Base._checkbounds(Base.trailingsize(V, n), I[n]) ||
Base.throw_boundserror(V, I))
end
unsafe_getindex(V, I...)
end
s1 = sub(Float64[1, 2], 1:2)
# println(@code_typed getindex4(s1, 1))
# println(@code_typed getindex3(s1, 1))
# println(@code_typed getindex2(s1, 1))
# println(@code_typed getindex(s1, 1))
# @code_llvm getindex2(s1, 1)
@time_func(getindex4, s1, 1)
@time_func(getindex3, s1, 1)
@time_func(getindex2, s1, 1)
@time_func(getindex, s1, 1)