Julia Low Pass Filter much slower than identical code in Python ??

978 views
Skip to first unread message

MLicer

unread,
Sep 12, 2016, 5:53:01 AM9/12/16
to julia-users

Dear all,

i've written a low-pass filter in Julia and Python and the code in Julia seems to be much slower (0.800 sec in Julia vs 0.000 sec in Python). I must be coding ineffieciently, can anyone comment on the two codes below?

Julia:

using PyPlot, DSP

# generate data:
x
= linspace(0,30,1e4)
sin_noise
(arr) = sin(arr) + rand(length(arr))

# create filter:
designmethod
= Butterworth(5)
ff
= digitalfilter(Lowpass(0.02),designmethod)
@time yl = filtfilt(ff, sin_noise(x))

Python:

from scipy import signal
import numpy as np
import cProfile, pstats

def sin_noise(arr):
   
return np.sin(arr) + np.random.rand(len(arr))

def filterSignal(b,a,x):
   
return signal.filtfilt(b, a, x, axis=-1)

def main():
   
# generate data:
    x
= np.linspace(0,30,1e4)
    y
= sin_noise(x)
    b
, a = signal.butter(5, 0.02, "lowpass", analog=False)
    ff
= filterSignal(b,a,y)

    cProfile
.runctx('filterSignal(b,a,y)',globals(),{'b':b,'a':a,'y':y},filename='profileStatistics')

    p
= pstats.Stats('profileStatistics')
    printFirstN
= 5
    p
.sort_stats('cumtime').print_stats(printFirstN)

if __name__=="__main__":
    main
()


Thanks very much for any replies!
Auto Generated Inline Image 1

randm...@gmail.com

unread,
Sep 12, 2016, 6:48:30 AM9/12/16
to julia-users
The Julia code takes 0.000535 seconds for me on the second run -- during the first run, Julia has to compile the method you're timing. Have a look at the performance tips for a more in depth explanation.

MLicer

unread,
Sep 12, 2016, 6:52:58 AM9/12/16
to julia-users
Indeed it does! I thought JIT compilation takes place prior to execution of the script. Thanks so much, this makes sense now!

Output:
first call:   0.804573 seconds (1.18 M allocations: 53.183 MB, 1.43% gc time)
repeated call:  0.000472 seconds (217 allocations: 402.938 KB)

Thanks again,

Cheers!

Stefan Karpinski

unread,
Sep 12, 2016, 11:17:58 AM9/12/16
to Julia Users
JIT = Just In Time, i.e. the first time you use the code.

Matjaz Licer

unread,
Sep 13, 2016, 3:10:02 AM9/13/16
to julia...@googlegroups.com
Makes sense, thanks :-)

Stefan Karpinski

unread,
Sep 13, 2016, 2:28:22 PM9/13/16
to Julia Users
As an aside, most JIT runtimes actually don't compile code until after it has been run a few thousand times and seems hot – before that they interpret it. Julia is unusual in that it fully JIT compiles almost all code (except for some top-level expressions which are interpreted) before running it at all. That's technically what JIT originally meant, but not what most systems do since they often need profile information from execution in order to compile the code, whereas Julia can infer this information in advance.
Reply all
Reply to author
Forward
0 new messages