From d502c09d4d4d9ac4a3e6baa5f2193bc1b7dc2d27 Mon Sep 17 00:00:00 2001 From: Philip Swannell Date: Tue, 19 Jan 2021 16:58:26 +0000 Subject: [PATCH] Minor refactoring --- src/rankcorr.jl | 22 ++++++++++++---------- src/speedtests.jl | 15 +++++++++------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/rankcorr.jl b/src/rankcorr.jl index 05b9cff..70d1568 100644 --- a/src/rankcorr.jl +++ b/src/rankcorr.jl @@ -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 @@ -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 @@ -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: @@ -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) @@ -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] diff --git a/src/speedtests.jl b/src/speedtests.jl index cf300c5..74406f8 100644 --- a/src/speedtests.jl +++ b/src/speedtests.jl @@ -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 -------------------------------------------------- ``` """