Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
PGS62 committed Jan 19, 2021
1 parent d5d4cb4 commit d502c09
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
22 changes: 12 additions & 10 deletions src/rankcorr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ end
Merge (sorting while doing so) two chunks of x: x[lo:m] and x[(m+1):hi] to form x[lo:hi].
Assumes chunks x[lo:m-1] and x[m:hi] are already sorted. Afterwards x[lo:hi] is sorted.
Returns the number of swaps required.
Returns the number of swaps that the bubblesort algorithm would require.
"""
function merge!(x::RealVector, lo::Int64, m::Int64, hi::Int64, buffer::RealVector)
nswaps = 0
Expand Down Expand Up @@ -182,13 +182,13 @@ function merge!(x::RealVector, lo::Int64, m::Int64, hi::Int64, buffer::RealVecto
end

"""
countties(x::RealVector,from::Int64,to::Int64)
countties(x::RealVector,lo::Int64,hi::Int64)
Assumes `x` is sorted. Returns the number of ties within `x[from:to]`.
Assumes `x` is sorted. Returns the number of ties within `x[lo:hi]`.
"""
function countties(x::RealVector, from::Int64, to::Int64)
function countties(x::RealVector, lo::Int64, hi::Int64)
thistiecount, result = 0, 0
for i (from + 1):to
for i (lo + 1):hi
if x[i] == x[i - 1]
thistiecount += 1
elseif thistiecount > 0
Expand All @@ -204,7 +204,7 @@ function countties(x::RealVector, from::Int64, to::Int64)
end

"""
speedtestmergesort(N=2000)
speedtestmergesort(n=2000)
Method to determine the best (i.e. fastest) value of `small_threshold` to method `mergesort!`.
Of the powers of 2 tested, 64 seems to maximise speed:
Expand All @@ -230,15 +230,17 @@ julia> KendallTau.speedtestmergesort()
432.499 μs (12 allocations: 74.72 KiB)
```
"""
function speedtestmergesort(N=2000)
function speedtestmergesort(n=2000)
for i = 2:10
println((N, (2^i)))
@btime KendallTau.mergesort!(randn(MersenneTwister(1), $N), 1, $N, (2^$i))
println((n, (2^i)))
@btime KendallTau.mergesort!(randn(MersenneTwister(1), $n), 1, $n, (2^$i))
end
end

using LoopVectorization
# corkendallnaive, a naive implementation, is faster than corkendall for very small number
# of elements (< 25 approx) but probably not worth having corkendall call corkendallnaive in that case.
# It does not seem to be possible to use LoopVectorization to speed up this method: "LoadError: Don't know how to handle expression"
"""
corkendallnaive(x::RealVector, y::RealVector)
Expand All @@ -251,7 +253,7 @@ function corkendallnaive(x::RealVector, y::RealVector)
if length(y) n error("Vectors must have same length") end

numerator, tiesx, tiesy = 0, 0, 0
for i 2:n,j 1:(i - 1)
for i 2:n, j 1:(i - 1)
k = sign(x[i] - x[j]) * sign(y[i] - y[j])
if k == 0
if x[i] == x[j]
Expand Down
15 changes: 9 additions & 6 deletions src/speedtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,20 @@ Prints comparisons of execution speed.
```
julia>using KendallTau, StatsBase
julia>speedtest([StatsBase.corkendall,KendallTau.corkendall,KendallTau.corkendallthreads_v2],2000,10)
###################################################################
Executing speedtest 2021-01-18T16:28:33.986
Executing speedtest 2021-01-19T16:26:29.883
size(matrix1) = (2000, 10)
StatsBase.corkendall(matrix1)
35.893 ms (451 allocations: 5.54 MiB)
33.503 ms (451 allocations: 5.54 MiB)
KendallTau.corkendall(matrix1)
6.903 ms (3448 allocations: 10.52 MiB)
6.172 ms (1918 allocations: 7.82 MiB)
Speed ratio KendallTau.corkendall vs StatsBase.corkendall: 5.428169574078446
Ratio of memory allocated KendallTau.corkendall vs StatsBase.corkendall: 1.4125820189187552
KendallTau.corkendallthreads_v2(matrix1)
2.112 ms (3764 allocations: 10.56 MiB)
all(myapprox.(results[2:end], results[1:end - 1], 1.0e-14)) = true
1.878 ms (2234 allocations: 7.86 MiB)
Speed ratio KendallTau.corkendallthreads_v2 vs StatsBase.corkendall: 17.83603066439523
Ratio of memory allocated KendallTau.corkendallthreads_v2 vs StatsBase.corkendall: 1.4198018874681153
Results from all 3 functions identical? true
--------------------------------------------------
```
"""
Expand Down

0 comments on commit d502c09

Please sign in to comment.