Hello,
As a simple test of Mata vs. Matlab performance, I wrote a Mata function to count from 1 to "n" and an analogous Matlab function. Matlab appears to be much faster than Mata - about 60x faster! For example, when I ask Matlab to count to 1 billion, it takes about 1 second and when I ask Mata to count to 1 billion, it takes about 60 seconds.
Here is the .mata file (saved as "timerfunction.mata"):
mata
void function timerfunction(real scalar n){
real scalar value
value=0
timer_clear()
timer_on(1)
for(i=1;i<=n;i++){
value=value+1
}
timer_off(1)
printf("Mata counted to %5.0g and it took %6.2f seconds.",value,timer_value(1)[1])
}
mata mosave timerfunction()
end
And here is the result of running with n=10^9:
run timerfunction.mata
mata: timerfunction(10^9)
Mata counted to 1.0e+09 and it took 67.90 seconds.
Here is the analogous .m file (saved to "timerfunction.m"):
function timerfunction(n)
value=0;
tic;
for i=1:n
value=value+1;
end
totaltime=toc;
fprintf('Matlab counted to %5.0g in %6.2f seconds.\n',value,totaltime)
end
And here is the result of running with n=10^9:
>> timerfunction(10^9)
Matlab counted to 1e+09 in 1.22 seconds.
Any thoughts on why Mata is performing so poorly compared to Matlab?
Thank you!
As a simple test of Mata vs. Matlab performance, I wrote a Mata function to count from 1 to "n" and an analogous Matlab function. Matlab appears to be much faster than Mata - about 60x faster! For example, when I ask Matlab to count to 1 billion, it takes about 1 second and when I ask Mata to count to 1 billion, it takes about 60 seconds.
Here is the .mata file (saved as "timerfunction.mata"):
mata
void function timerfunction(real scalar n){
real scalar value
value=0
timer_clear()
timer_on(1)
for(i=1;i<=n;i++){
value=value+1
}
timer_off(1)
printf("Mata counted to %5.0g and it took %6.2f seconds.",value,timer_value(1)[1])
}
mata mosave timerfunction()
end
And here is the result of running with n=10^9:
run timerfunction.mata
mata: timerfunction(10^9)
Mata counted to 1.0e+09 and it took 67.90 seconds.
Here is the analogous .m file (saved to "timerfunction.m"):
function timerfunction(n)
value=0;
tic;
for i=1:n
value=value+1;
end
totaltime=toc;
fprintf('Matlab counted to %5.0g in %6.2f seconds.\n',value,totaltime)
end
And here is the result of running with n=10^9:
>> timerfunction(10^9)
Matlab counted to 1e+09 in 1.22 seconds.
Any thoughts on why Mata is performing so poorly compared to Matlab?
Thank you!
Comment