matlab vs julia slower fft

579 views
Skip to first unread message

baillot maxime

unread,
May 13, 2016, 11:41:21 AM5/13/16
to julia-users
Hello

I'm using julia to do some fft in for loops and I notice that julia fft is slower than matlab fft 2.18s for matlab and 3.06s for julia.

I did some test to see what's going on.

I first used the FFTW.set_num_threads(n) to spead up the code tat I'm using and noticed that the code was slower with 4 threads than with 1. But not that much.

I was thinking that the FFTW.set_num_threads(n) had a bug and I did this code to check it

nbp = 2^12;

M = rand(nbp,nbp);

println("Threds 1")
FFTW.set_num_threads(1)
@time fft(M)


println("Threds 2")
FFTW.set_num_threads(2)
@time fft(M)


println("Threds 3")
FFTW.set_num_threads(3)
@time fft(M)


println("Threds 4")
FFTW.set_num_threads(4)
@time fft(M)

after the second run I got good result

 Threds 1
  2.213230 seconds (76 allocations: 512.003 MB, 2.06% gc time)
Threds 2
  1.926431 seconds (76 allocations: 512.003 MB, 0.31% gc time)
Threds 3
  1.716771 seconds (76 allocations: 512.003 MB, 0.22% gc time)
Threds 4
  1.695626 seconds (76 allocations: 512.003 MB, 0.20% gc time)

So everything is fine with the FFTW threads.


but if I do the same fft on matlab I have 

>> nbp = 2^12;

M = rand(nbp,nbp);
>> tic; fft(M); toc
Elapsed time is 1.008783 seconds.
>> tic; fft(M); toc
Elapsed time is 0.154519 seconds.


So maybe I did something wrong because I don't understand why julia is slower than matlab (the julia dev say that it's a faster language).
And more than that julia and matlab use the same FFTW no? so why this time difference?

Maybe some one have a idea or an explanation.

Thank you in advance.

PS: I know that there is the plan_fft() function but I don't want to use it 

Steven G. Johnson

unread,
May 13, 2016, 12:08:13 PM5/13/16
to julia-users
I think Matlab caches precomputed plans (FFTW "wisdom") for power-of-two sizes, maybe that is making the difference here.   Also, for real arrays Matlab might be using FFTW's real-data FFT by default.  Try it for a complex input array in both cases, and try it with and without precomputed plans in Julia.

baillot maxime

unread,
May 13, 2016, 4:55:32 PM5/13/16
to julia-users
I remember seeing some thing about the power of sizes array.
I will try to the complex array and I will give you the results

Thank you!

baillot maxime

unread,
May 13, 2016, 6:51:52 PM5/13/16
to julia-users
I did quickly some more test

for matlab it's the same time xomplex or real data array

nbp = 2^12;
M = rand(nbp);

tic
fft(M);
toc

Mp = exp(1i*M);

tic
fft(Mp);
toc

I got 
Elapsed time is 0.041000 seconds.
Elapsed time is 0.054349 seconds.

I'm on a different computer now so the time is different from previously


In julia I used this code to do the test
FFTW.set_num_threads(8)

nbp = 2^12;

M = rand(nbp,nbp)

@time rfft(M)

Mb = exp(im*M)

@time fft(Mb)


pfft=plan_fft(Mb);

@time pfft*Mb

rfft -> 0.060962 seconds (99 allocations: 128.067 MB, 1.76% gc time)
fft -> 0.556133 seconds (73 allocations: 256.003 MB, 0.13% gc time)
plan_fft -> 0.487461 seconds (9 allocations: 256.000 MB, 1.45% gc time)


and in did the rfft is faster, same time as matlab. Strangely the plan_fft take the same time as a normal fft (but maybe it's normal and I don't understand how it's working)


I also did for a sizes of 2^12-1. In matlab the time was the same but in julia all the fft was around 0.1s


Anyway, thanks again for the answer. Now I have a better understanding of what's going on. I think matlab have some shenanigans in the background! hahahaha

Steven G. Johnson

unread,
May 13, 2016, 7:21:06 PM5/13/16
to julia-users
Do plan_fft with the FFTW_PATIENT flag, since that's probably what Matlab's cached plans use

Steven G. Johnson

unread,
May 13, 2016, 7:25:05 PM5/13/16
to julia-users
Also, you need to use fftn in Matlab. The Matlab fft function only transforms the columns of the array. Julia's fft function transforms all the dimensions by default (although you can tell it to transfer just the columns if you want).

baillot maxime

unread,
May 14, 2016, 4:36:44 AM5/14/16
to julia-users
Yes, I total forgot the fact that I need to use fftn in matlab my bad.

After changing that and using an array of 2^12-1 I have the same time between Julia and Matlab.

Also is it the right way to use plan_fft with the flag FFTW.PATIENT?  pfft=plan_fft(Mb;flags=FFTW.PATIENT);

and for one dimensional array (that the ffit I do in my main program) the time look like the same between fft of complex.

Anyway so that look like the time difference between my julia program and matlab program come from some where else. I will try to use a plan_fft to see.

Thank you sorry for the rookie mistake....... 
Reply all
Reply to author
Forward
0 new messages