Skip to content

Instantly share code, notes, and snippets.

View carlobaldassi's full-sized avatar

Carlo Baldassi carlobaldassi

View GitHub Profile
import Base: @ngenerate, @nloops, @nref, @ncall, @ntuple, @nif, @nexprs
import Base: start, done, next
# this generates types like this:
# immutable SubInd_3 <: SubInds{3}
# I_1::Int
# I_2::Int
# I_3::Int
# end
# they are used as iterator states
@carlobaldassi
carlobaldassi / broadcast_cache_perf.jl
Last active August 29, 2015 13:57
Improve broadcast cache performance
let broadcast_cache = Dict{Function,Dict{Int,Dict{Int,Function}}}()
global broadcast!
function broadcast!(f::Function, B, As::Union(Array,BitArray)...)
nd = ndims(B)
narrays = length(As)
idx_f = Base.ht_keyindex2(broadcast_cache, f)
if idx_f < 0
idx_f = -idx_f
Base._setindex!(broadcast_cache, Dict{Int,Dict{Int,Function}}(), f, idx_f)
@carlobaldassi
carlobaldassi / minvoke.jl
Last active August 29, 2015 13:57
Simplified invoke interface via macro
macro invoke(ex)
Meta.isexpr(ex, :call) || error("invoke macro syntax error")
isa(ex.args[1], Symbol) || error("invoke macro syntax error")
fname = ex.args[1]
types = [Meta.isexpr(a, :(::)) ? a.args[2] : Expr(:call, :typeof, a) for a in ex.args[2:end]]
args = [Meta.isexpr(a, :(::)) ? a.args[1] : a for a in ex.args[2:end]]
Expr(:call, :invoke, fname, Expr(:tuple, types...), args...)
end
@carlobaldassi
carlobaldassi / glpkblog1.jl
Created April 9, 2014 20:37
Demo code for Stéphan Laurent blog post comment
import GLPK # NOTE: it's better to use 'import' with GLPK,
# rather than 'using'
μ = [1/7, 2/7, 4/7]
ν = [1/4, 1/4, 1/2]
n = length(μ)
D = 1 .- eye(n) # NOTE: starting from julia v0.3, you won't be
# allowed to use '-' here, you need to
# use '.-'
@carlobaldassi
carlobaldassi / regex_rsearch.jl
Created April 9, 2014 23:30
Naïve reverse rsearch
function Base.rsearch(str::Union(ByteString,SubString), re::Regex, idx::Integer = endof(str))
sidx = 1
lastr = 0:-1
while sidx <= endof(str)
r = search(str, re, sidx)
isempty(r) && return lastr
last(r) > last(lastr) && (lastr = r)
sidx = nextind(str, first(r))
end
return lastr
@carlobaldassi
carlobaldassi / slowremote.jl
Created April 29, 2014 09:43
Slowdown of a function executed remotely (Julia issue #6686)
module SlowRemoteExecution
type W
J::Vector{BitVector}
W(N::Int, K::Int) = new([randbool(N) for k=1:K])
end
function inner(ws::Vector{W}, X::Vector{BitVector})
r = 0
@time begin
@carlobaldassi
carlobaldassi / bitlogicalbench.jl
Created January 7, 2015 09:50
logical indexing benchmark
function bench()
b = bitrand(500, 500, 300)
i = bitrand(500, 500, 300)
t = vec(bitrand(sum(i)))
c = bitunpack(b)
j = bitunpack(i)
y = bitunpack(t)
println("logical getindex A[I]")
@carlobaldassi
carlobaldassi / nullablerand.jl
Last active August 29, 2015 14:13
NullableVector performance with random access
module NV
immutable NullableVector{T}
isnull::BitVector
values::Vector{T}
end
@inline Base.length(nv::NullableVector) = length(nv.isnull)
@inline function Base.getindex(nv::NullableVector{Float64}, i::Integer)
@carlobaldassi
carlobaldassi / PartSort.jl
Last active August 29, 2015 14:18
partial select
module PartSort
using Base.Order
import Base: sort!, Sort.Algorithm
export PartialQuickSort
immutable PartialQuickSort <: Algorithm
k::Int
@carlobaldassi
carlobaldassi / intvec_abstractsmart.txt
Last active August 29, 2015 14:20
abstractsmart benchmarking
abstractvector getindex A[I]
---------------------
A::BitArray I::Vector{Int}
elapsed time: 0.271078954 seconds (4 MB allocated)
A::Array{Bool} I::Vector{Int}
elapsed time: 0.096585028 seconds (35 MB allocated)
A::Array{Float64} I::Vector{Int}
elapsed time: 0.267021423 seconds (286 MB allocated)
abstractvector setindex! A[I]=X