The type instability looks like a bug in Julia to me, filed issue: https://github.com/JuliaLang/julia/issues/13665
On Sunday, October 18, 2015 at 2:54:04 PM UTC+2, Andras Niedermayer wrote: > > There is a type instability (see here > <http://docs.julialang.org/en/release-0.4/manual/performance-tips/#write-type-stable-functions>) > > that slows down your code. > > @code_warntype Jakes_Flat( 926, 1e-6, 50000, 0, 1, 0 ) > shows that variable h has type Any. > > I managed to track down the type instability to the concatenation > [ phi_N pi/(N0+1)*[1:N0]'] > > You can get around the type instability with this modification: > function Jakes_Flat( fd, Ts, Ns, t0 = 0, E0 = 1, phi_N = 0 ) > # Inputs: > # > # Outputs: > N0 = 8; # As suggested by Jakes > N = 4*N0+2; # An accurate approximation > wd = 2*pi*fd; # Maximum Doppler frequency > t = t0 + [0:Ns-1;]*Ts; > tf = t[end] + Ts; > coswt = [ sqrt(2)*cos(wd*t'); 2*cos(wd*cos(2*pi/N*[1:N0;])*t') ] > temp = zeros(1,N0+1) > temp[1,2:end] = pi/(N0+1)*[1:N0;]' > temp[1,1] = phi_N > h = E0/sqrt(2*N0+1)*exp(im*temp ) * coswt > return h, tf; > end > > This is faster 30% faster than before, but still not as fast as Matlab. > Maybe someone else knows the reason for the speed difference (I guess it's > due to temporary arrays). > > On Sunday, October 18, 2015 at 1:17:50 PM UTC+2, Vishnu Raj wrote: >> >> Although Julia homepage shows using Julia over Matlab gains more in >> performance, my experience is quite opposite. >> I was trying to simulate channel evolution using Jakes Model for wireless >> communication system. >> >> Matlab code is: >> function [ h, tf ] = Jakes_Flat( fd, Ts, Ns, t0, E0, phi_N ) >> %JAKES_FLAT >> % Inputs: >> % fd, Ts, Ns : Doppler frequency, sampling time, number of samples >> % t0, E0 : initial time, channel power >> % phi_N : initial phase of the maximum Doppler frequeny >> % sinusoid >> % >> % Outputs: >> % h, tf : complex fading vector, current time >> >> if nargin < 6, phi_N = 0; end >> if nargin < 5, E0 = 1; end >> if nargin < 4, t0 = 0; end >> >> N0 = 8; % As suggested by Jakes >> N = 4*N0 + 2; % an accurate approximation >> wd = 2*pi*fd; % Maximum Doppler frequency[rad] >> t = t0 + [0:Ns-1]*Ts; % Time vector >> tf = t(end) + Ts; % Final time >> coswt = [ sqrt(2)*cos(wd*t); 2*cos(wd*cos(2*pi/N*[1:N0]')*t) ]; >> h = E0/sqrt(2*N0+1)*exp(j*[phi_N pi/(N0+1)*[1:N0]])*coswt; >> end >> Enter code here... >> >> My call results in : >> >> tic; Jakes_Flat( 926, 1E-6, 50000, 0, 1, 0 ); toc >> Elapsed time is 0.008357 seconds. >> >> >> My corresponding Julia code is >> function Jakes_Flat( fd, Ts, Ns, t0 = 0, E0 = 1, phi_N = 0 ) >> # Inputs: >> # >> # Outputs: >> N0 = 8; # As suggested by Jakes >> N = 4*N0+2; # An accurate approximation >> wd = 2*pi*fd; # Maximum Doppler frequency >> t = t0 + [0:Ns-1]*Ts; >> tf = t[end] + Ts; >> coswt = [ sqrt(2)*cos(wd*t'); 2*cos(wd*cos(2*pi/N*[1:N0])*t') ] >> h = E0/sqrt(2*N0+1)*exp(im*[ phi_N pi/(N0+1)*[1:N0]']) * coswt >> return h, tf; >> end >> # Saved this as "jakes_model.jl" >> >> >> My first call results in >> julia> include( "jakes_model.jl" ) >> Jakes_Flat (generic function with 4 methods) >> >> julia> @time Jakes_Flat( 926, 1e-6, 50000, 0, 1, 0 ) >> elapsed time: 0.65922234 seconds (61018916 bytes allocated) >> >> julia> @time Jakes_Flat( 926, 1e-6, 50000, 0, 1, 0 ) >> elapsed time: 0.042468906 seconds (17204712 bytes allocated, 63.06% gc >> time) >> >> For first execution, Julia is taking huge amount of time. On second call, >> even though Julia take considerably less(0.042468906 sec) than first( >> 0.65922234 sec), it's still much higher to Matlab(0.008357 sec). >> I'm using Matlab R2014b and Julia v0.3.10 on Mac OSX10.10. >> >> - vish >> >
