Skip to content

Commit 63dfbc2

Browse files
committed
Do some microptimization
1 parent 965f2dc commit 63dfbc2

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
99
Tullio = "bc48ee85-29a4-5162-ae0b-a64e1601d4bc"
1010

1111
[compat]
12-
julia = "1"
1312
ColorTypes = "0.11"
1413
FFTW = "1.4"
1514
Tullio = "0.3"
15+
julia = "1"
1616

1717
[extras]
1818
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

examples/benchmarks/bin.jl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
using BenchmarkTools, MicroscopyTools
22

3+
function f(x)
4+
@info "Random data with size $(size(x))"
5+
print("Tullio: ")
6+
@btime bin2($x)
7+
print("CartesianIndices: ")
8+
@btime bin($x, (2,2))
9+
end
10+
311
function main()
12+
@info "Binning by (2,2)"
13+
x = randn(Float32, (256, 512))
14+
f(x)
15+
16+
x = randn(Float32, (8192, 7113))
17+
f(x)
18+
19+
x = randn((97, 32))
20+
f(x)
421

522
x = randn((3097, 113, 32))
6-
@warn "random data with size $(size(x))"
7-
@btime bin2($x)
8-
@btime bin($x, (2,2))
23+
f(x)
24+
25+
x = randn((23, 31, 8))
26+
f(x)
927

28+
x = randn(Float32, (3097, 4096, 32))
29+
f(x)
1030

11-
@warn "random data with size $(size(x))"
12-
x = randn((3097, 1113))
13-
@btime bin2($x)
14-
@btime bin($x, (2,2))
31+
return
1532
end
1633

1734
main()

src/binning.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,36 @@ function bin(arr::AbstractArray{T, N}, binning::NTuple{M, Int}) where {T, N, M}
5050
# largest size which is possible according to binning
5151
out = similar(arr, size(arr) binning)
5252

53+
out_indices = CartesianIndices(out)
54+
z = zero(T)
55+
neighbours = CartesianIndices(binning)
5356
# iterate over the indices of the out put array
54-
for inds in CartesianIndices(out)
57+
#@inbounds and @simd make it faster
58+
@inbounds @simd for inds in out_indices
5559
# now we start to sum within binning
56-
sum = zero(T)
60+
s = z
5761
# sum all neighbours
58-
for neighbours in CartesianIndices(binning)
59-
pos = binning .* Tuple(inds) .- Tuple(neighbours) .+ 1
60-
sum += arr[pos...]
62+
base_inds = binning .* Tuple(inds) .+ 1
63+
for neighbour in neighbours
64+
pos = base_inds .- Tuple(neighbour)
65+
s += arr[pos...]
6166
end
6267
# assign the sum
63-
out[inds] = sum
68+
out[inds] = s
6469
end
6570
return out
6671
end
6772

6873

74+
# was slightly slower with function barrier
75+
# Function barrier.
76+
# """
77+
# function _bin_loop!(out::AbstractArray{T, N},
78+
# arr::AbstractArray{T, N},
79+
# binning::NTuple{N, Int}) where {T, N}
80+
## end
81+
82+
6983
"""
7084
bin2(arr)
7185

0 commit comments

Comments
 (0)