dysonance / Indicators.jl

Financial market technical analysis & indicators in Julia
Other
216 stars 61 forks source link

Running Statistics Are Slow #22

Closed Sinansi closed 4 years ago

Sinansi commented 4 years ago

Hello,

I just wish to report that Running Statistics of this library are super slow.

On an array of 4000 elements: Running Correlation of Indicators.jl takes: 0.085305 seconds Running Correlation of TA-lib takes: 0.000577 seconds

In my application, I loop hundreds of times over running statistics, thus the difference in speed has an impact on the overall performance.

I thought Indicators.jl is faster since it is written in Julia and does not use wrappers like TA-lib do.

Thank you!

dysonance commented 4 years ago

@Sinansi Thanks for bringing this up, I will definitely look into it and try to improve the performance. I have some ideas of how recent changes might have had an impact on performance. Would you mind telling me what Julia version and Indicators.jl version you're using? Would help me diagnose what could be causing the performance issues.

Sinansi commented 4 years ago

@dysonance firstly, I would like to thank you so much for your politeness and caring.

The versions I am currently using as follows:

"Indicators" => v"0.7.0"

julia> versioninfo() Julia Version 1.2.0 Commit c6da87ff4b (2019-08-20 00:03 UTC) Platform Info: OS: Windows (x86_64-w64-mingw32) CPU: Intel(R) Xeon(R) CPU E5-2695 v2 @ 2.40GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-6.0.1 (ORCJIT, ivybridge) Environment: JULIA_EDITOR = "C:\Users\Sannan\AppData\Local\atom\app-1.42.0\atom.exe" -a JULIA_HOME = C:\Users\Sannan\AppData\Local\Julia-1.2.0 JULIA_NUM_THREADS = 24

dysonance commented 4 years ago

@Sinansi Just to follow up as I continue researching this a bit more, could you give me some insight into how you're calculating your benchmark times so that I might be able to replicate?

It may not be relevant to your benchmarking practices, but I've found some epsilon/error in Julia's timing measurements that could potentially add some fat around timing measurements, depending on how they're taken. As an admittedly flawed minimal example, at the millisecond level, I've found a buffer of ~2.5 milliseconds around time measurements:

import Dates.now
import Statistics.mean
timings = [(t0 = now(); sleep(0.001); t1 = now(); t1 - t0) for _ in 1:1000]
#1000-element Array{Dates.Millisecond,1}:
# 2 milliseconds
# 3 milliseconds
# 2 milliseconds
# 3 milliseconds
# 2 milliseconds
# 2 milliseconds
# 3 milliseconds
# 3 milliseconds
# 2 milliseconds
# 3 milliseconds
# 2 milliseconds
# ⋮
# 3 milliseconds
# 2 milliseconds
# 3 milliseconds
# 3 milliseconds
# 2 milliseconds
# 3 milliseconds
# 2 milliseconds
# 3 milliseconds
# 3 milliseconds
# 2 milliseconds
# 2 milliseconds
mean([dt.value for dt in timings])
#2.568

To touch on your earlier point about Julia being faster because it doesn't rely on wrappers, one thing to keep in mind is that all the TA-Lib code has been implemented and fine-tuned at a very low level in C, and has been optimized both in the source code and by the C compiler. Python may need to have wrappers for this library, but ultimately the underlying computations are compiled and optimized ahead of time. Julia can keep up performance-wise in some comparable areas, but given the amount of just-in-time compiling that takes place, matching performance to these streamlined libraries can be difficult. (This is especially the case when functions/methods in Julia are defined to support several different types of inputs in order to permit more flexible prototyping, as is often the case with this package.)

In the meantime, I'll definitely keep experimenting to see what performance gains I can eek out!

Sinansi commented 4 years ago

@dysonance Thanks alot for your reply. I am new to Julia so I am not using a professional benchmarking. I am only using the @btime micro. I didn't know that TA-Lib is based on ahead of time compilation at a very low level optimization, thats probably a very good reason why it is faster than Julia.

I am glad to hear that this library is improving as it is the main technical analysis library in the Julia ecosystem, as well as the main library at my work. Keep up the good work!

dysonance commented 4 years ago

@Sinansi I'm about to release a version that includes some performance improvements, so I'm going to close this for now. Let me know if you still experience issues that deserve re-visiting and we can consider re-opening and doing a deep dive into possible performance tweaks we might be able to put in place.

Sinansi commented 4 years ago

@dysonance Array Views in Julia create copies of the Array and this kills performance. This issue has been solved in Julia 1.5. Hopefully, that may improve performance. https://github.com/JuliaLang/julia/issues/14955#issue-131848906