Skip to content

Instantly share code, notes, and snippets.

View johnmyleswhite's full-sized avatar

John Myles White johnmyleswhite

View GitHub Profile
julia> import BenchmarkTools: @btime
julia> @inline _logistic_bounds(x::Float16) = (Float16(-16.64), Float16(7.625))
_logistic_bounds (generic function with 1 method)
julia> @inline _logistic_bounds(x::Float32) = (-103.27893f0, 16.635532f0)
_logistic_bounds (generic function with 2 methods)
julia> @inline _logistic_bounds(x::Float64) = (-744.4400719213812, 36.7368005696771)
_logistic_bounds (generic function with 3 methods)
│ Row │ expr_type │ count │ percentage │
│ │ Any │ Int64 │ Float64 │
├─────┼────────────────────────────────┼────────┼─────────────┤
│ 1 │ ExprKind{:outer} │ 1 │ 0.000226006 │
│ 2 │ ExprKind{:/=} │ 1 │ 0.000226006 │
│ 3 │ ExprKind{:typed_vcat} │ 1 │ 0.000226006 │
│ 4 │ ExprKind{:.+=} │ 1 │ 0.000226006 │
│ 5 │ ExprKind{:%=} │ 3 │ 0.000678017 │
│ 6 │ ExprKind{:⊻=} │ 7 │ 0.00158204 │
│ 7 │ ExprKind{:.=} │ 8 │ 0.00180804 │
import RCall: @R_str
function runtimes(n_reps)
times = Array{Float64}(undef, n_reps)
x = rand(10_000_000)
for i in 1:n_reps
times[i] = @elapsed sum(x)
end
times
end
> library("stringr")
> str_sub("ñ", start = -1)
[1] "̃"
> str_sub("ñ", start = -1)
[1] "ñ"
function prob_f64(n)
s = 0
for _ in 1:n
z_i = rand(UInt)
x_i = reinterpret(Float64, z_i)
s += Int(x_i == x_i + 1.0)
end
s, n
end
function prob(n)
s = 0
for _ in 1:n
x_i = rand(UInt)
s += Int(Float64(x_i) === Float64(x_i + 1))
end
s, n
end
prob(1_000_000)
function fixed_point(f, x0)
x, x_old = f(x0), x0
n = 1
while x !== x_old
x, x_old = f(x), x
n += 1
end
(x, n)
end
library("pwr")
library("ggplot2")
n_sims <- 1000L
n <- 10L
mu <- 0.5
sigma <- 1
true_power <- power.t.test(
n = n,
@johnmyleswhite
johnmyleswhite / missing_values.jl
Created June 27, 2018 12:28
Perf of Missing Values in Julia 0.7
using BenchmarkTools
n = 10_000_000
x = rand(n)
y = [ifelse(iseven(i), missing, x[i]) for i in 1:n]
sum(x)
sum(y)
# 0.6 Approach
using BenchmarkTools
using NullableArrays
n = 10_000_000
x = rand(n)
y = NullableArray{Float64}(n)
for i in 1:n
if !iseven(i)