Использование @time в Юлии дает удивительные результаты - PullRequest
1 голос
/ 06 октября 2019

Я не уверен, почему это происходит.

using Formatting: printfmt


function within(x,y)
    if x * x + y * y <= 1 
        true 
    else 
        false 
    end
end

function π_estimation_error(estimated_value)
    pi_known = 3.1415926535897932384626433
    return abs((pi_known - estimated_value) / 100)    
end

function estimate_π_1(n)
    count = 0
    for i = 1:n
      if within(rand(), rand())
        count = count + 1
      end
    end    
    pi_est = count/n*4
    printfmt("n: {} π estimated {:.8f}, error {:.10f}", n, pi_est, π_estimation_error(pi_est))
end

function estimate_π_2(n)
    rand_coords = rand(n, 2) .^ 2
    count = sum(rand_coords[:,1] + rand_coords[:,2] .<= 1)
    pi_est = count/n*4
    printfmt("n: {} π estimated {:.8f}, error {:.10f}", n, pi_est, π_estimation_error(pi_est))
end

number_of_experiments = 20000000

for i = 1:10
    print("1 :: ")
    @time estimate_π_1(number_of_experiments)
    print("2 :: ")
    @time estimate_π_2(number_of_experiments)
end

Как правильно получить последовательные результаты? Не уверен, почему это происходит. Числа распределения кажутся далеко не такими.

1 :: n: 20000000 π estimated 3.14188540, error 0.0000029275  0.507643 seconds (1.15 M allocations: 56.432 MiB, 8.75% gc time)
2 :: n: 20000000 π estimated 3.14141280, error 0.0000017985  0.786538 seconds (1.13 M allocations: 1.100 GiB, 13.17% gc time)
1 :: n: 20000000 π estimated 3.14118120, error 0.0000041145  0.054791 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14207560, error 0.0000048295  0.536932 seconds (196 allocations: 1.045 GiB, 14.11% gc time)
1 :: n: 20000000 π estimated 3.14119660, error 0.0000039605  0.054647 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14154040, error 0.0000005225  0.529361 seconds (196 allocations: 1.045 GiB, 14.04% gc time)
1 :: n: 20000000 π estimated 3.14188640, error 0.0000029375  0.054321 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14177120, error 0.0000017855  0.532848 seconds (196 allocations: 1.045 GiB, 14.01% gc time)
1 :: n: 20000000 π estimated 3.14191880, error 0.0000032615  0.055158 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14213220, error 0.0000053955  0.524499 seconds (196 allocations: 1.045 GiB, 14.02% gc time)
1 :: n: 20000000 π estimated 3.14161380, error 0.0000002115  0.054355 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14174220, error 0.0000014955  0.529431 seconds (196 allocations: 1.045 GiB, 14.17% gc time)
1 :: n: 20000000 π estimated 3.14178600, error 0.0000019335  0.054558 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14152500, error 0.0000006765  0.537786 seconds (196 allocations: 1.045 GiB, 13.89% gc time)
1 :: n: 20000000 π estimated 3.14163340, error 0.0000004075  0.055921 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14220380, error 0.0000061115  0.521758 seconds (196 allocations: 1.045 GiB, 14.19% gc time)
1 :: n: 20000000 π estimated 3.14092000, error 0.0000067265  0.054592 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14177460, error 0.0000018195  0.527376 seconds (196 allocations: 1.045 GiB, 14.10% gc time)
1 :: n: 20000000 π estimated 3.14171780, error 0.0000012515  0.054904 seconds (181 allocations: 6.711 KiB)
2 :: n: 20000000 π estimated 3.14136040, error 0.0000023225  0.528569 seconds (196 allocations: 1.045 GiB, 14.04% gc time)

Это происходит из-за некоторой оптимизации?

1 Ответ

3 голосов
/ 06 октября 2019

Я так понимаю, вы спрашиваете, почему первый запуск функции всегда намного медленнее и выделяет больше памяти, чем последующие?

Причина в том, что Юлия является компилируемым языком - поэтому при первом запуске любой функцииДжулия скомпилирует его в двоичный код, создав двоичные методы, соответствующие наиболее часто ожидаемым типам ввода. Для любых последующих вызовов этой функции Джулия увидит, что она уже сгенерировала двоичный код, и просто использует это.

...