Предложения по улучшению производительности в Юлии - PullRequest
3 голосов
/ 21 апреля 2020

Я делаю свою первую попытку перейти от Matlab к Джулии и обнаружил, что мой код улучшился в ~ 3 раза, но все еще думаю, что это еще не все, я не использую глобальные переменные в функции и предварительно выделил все используемые массивы (я думаю?). Если бы были какие-то мысли о том, как его можно ускорить, это было бы очень полезно, я полностью перевожу деньги даже при текущем улучшении, я думаю!

function word_sim(tau::Int, omega::Int, mu::Float64)
# inserts a word in position (tau+1), at each point creates a new word with prob mu
# otherwise randomly chooses a previously used. Runs the program until time omega

words = zeros(Int32, 1, omega) # to store the words
tests = rand(1,omega) # will compare mu to these
words[1] = 1; # initialize the words
next_word = 2 # will be the next word used
words[tau+1] = omega + 1; # max possible word so insert that at time tau
innovates = mu .> tests; # when we'll make a new word
for i = 2:tau # simulate the process
    if innovates[i] == 1 # innovate 
        words[i] = next_word
        next_word = next_word + 1
    else # copy
        words[i] = words[rand(1:(i-1))]
    end
end
# force the word we're interested in
for i = (tau+2):omega
    if innovates[i] == 1 # innovate 
        words[i] = next_word
        next_word = next_word + 1
    else # copy
        words[i] = words[rand(1:(i-1))]
    end
end
result = sum(words .== (omega + 1)); # count how many times our word occurred
return result
end

и когда я запусту его с этими значениями на моем P C

using Statistics
@time begin
nsim = 10^3;
omega = 100;
seed = [0:1:(omega-1);]; 
mu = 0.01; 

results = zeros(Float64, 1, length(seed));
pops = zeros(Int64, 1, nsim);
for tau in seed
    for jj = 1:nsim
        pops[jj] = word_sim(tau, omega, mu);
    end
    results[tau+1] = mean(pops);
end
end

это занимает ~ ~ .26 секунд. Или, возможно, мне лучше написать код на C ++? Джулия была моей первой реакцией, так как я слышал восторженные отзывы о ее синтаксисе, который, честно говоря, это fantasti c!

Любые комментарии с благодарностью.

...