using BenchmarkTools
using InteractiveUtils
using Random
using Statistics
Random.seed!(1234)TaskLocalRNG()
using BenchmarkTools
using InteractiveUtils
using Random
using Statistics
Random.seed!(1234)TaskLocalRNG()
The following function computes the share of simulated wages above a reservation wage.
wage_draws = 10.0 .+ randn(100_000)
function acceptance_rate_global(reservation_wage)
accepted = 0
for wage in wage_draws
accepted += wage >= reservation_wage
end
return accepted / length(wage_draws)
endacceptance_rate_global (generic function with 1 method)
@code_warntype and @btime.The following function computes total surplus from accepted wage offers.
function total_surplus_unstable(wages, reservation_wage)
total = 0
for wage in wages
if wage >= reservation_wage
total += wage - reservation_wage
end
end
return total
endtotal_surplus_unstable (generic function with 1 method)
@code_warntype to diagnose the function.The following OLS objective creates temporary vectors at every evaluation.
function ols_objective_allocating(beta, x, y)
fitted = beta[1] .+ beta[2] .* x
residuals = y .- fitted
return mean(residuals .^ 2)
end
n = 100_000
x = randn(n)
y = 1.0 .+ 2.0 .* x .+ randn(n)
beta = [1.0, 2.0]2-element Vector{Float64}:
1.0
2.0