這個實驗的目標是將Matlab和Julia的速度與下面的一小段代碼進行比較。關於Julia和Matlab之間時序比較的奇怪觀察
首先Matlab代碼:
>> t = 5000; n = 10000; x = 1:t*n;
>> x = reshape(x, t, n);
>> tic(); y1 = sum(x(:) .* x(:)); toc()
Elapsed time is 0.229563 seconds.
>> y1
y1 =
4.1667e+22
>> tic(); y2 = trace(x * x'); toc()
Elapsed time is 15.332694 seconds.
>> y2
y2 =
4.1667e+22
對戰中朱莉婭
julia> t = 5000; n = 10000; x = 1: t*n;
julia> x = reshape(x, t, n);
julia> tic(); y1 = sum(x[:].* x[:]); toc();
elapsed time: 1.235170533 seconds
julia> y1
-4526945843202100544
julia> tic();y2 = trace(x*x'); toc();
第二個沒完成的作業超過1分鐘。那麼朱莉婭怎麼了?這段代碼碰巧在Julia中運行速度較慢且溢出?我的風格有什麼問題嗎?我認爲從Matlab轉換到Julia的一個原因是速度,我曾經認爲Julia默認處理大數運算操作。現在,看起來這些不正確。有人可以解釋嗎?
參見http://docs.julialang.org/en/release-0.3/manual/performance-tips/和http://docs.julialang.org/en/release-0.3/manual/faq/#why- do-julia-use-native-machine-integer-arithmetic –
謝謝Isaiah! – user25004