Skip to content

Instantly share code, notes, and snippets.

View afniedermayer's full-sized avatar

Andras Niedermayer afniedermayer

View GitHub Profile
@afniedermayer
afniedermayer / isinferred.jl
Created August 25, 2017 13:26
`@isinferred` without evaluating function
module Foo
_args_and_call(args...; kwargs...) = (args[1:end-1], kwargs, args[end](args[1:end-1]...; kwargs...))
_args(args...; kwargs...) = (args[1:end-1], kwargs)
_call(args...; kwargs...) = args[end](args[1:end-1]...; kwargs...)
macro inferred(ex)
inference = inferred_impl(ex, true, __module__)
quote
let (inftypes, result) = $inference
rettype = isa(result, Type) ? Type{result} : typeof(result)
rettype == inftypes[1] || error("return type $rettype does not match inferred return type $(inftypes[1])")
import Base.Test: @inferred
macro inferred(ex)
if Meta.isexpr(ex, :ref)
ex = Expr(:call, :getindex, ex.args...)
end
Meta.isexpr(ex, :call)|| error("@inferred requires a call expression")
if isdefined(Main, :Base) && isdefined(Main.Base, :string) && applicable(Main.Base.string, ex)
string_ex = Main.Base.string(ex)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@afniedermayer
afniedermayer / bench.jl
Created September 29, 2015 09:48
Globals vs Const Globals vs Closures vs Call Overloading
a = 3
const b = 3
immutable Parameters
val::Int64
end
g(f, N) = f(N)
function f_glob(N)
@generated function mixed_vcat(x...)
mixed_vcat_impl(x...)
end
function mixed_vcat_impl(x...)
T = Base.promote_eltype(x...)
result = [:(counter = 1),
:(totallength = 0)]
for i = 1:length(x)
@afniedermayer
afniedermayer / Interpolation.jl
Created February 2, 2015 15:41
cubic hermite interpolation (needs to be sped up, cleaned up, documented...)
module Interpolation
# by Andras Niedermayer
# afniedermayer@gmail.com
export interpolation, interpolation_regular, pchip,
CubicHermiteInterp, getindex, inverse
using Grid
#using Debug
@afniedermayer
afniedermayer / sort_pairs.py
Last active August 29, 2015 14:13
sorting of tuple/pairs
import numpy as np
import copy, time, gc
temp=np.random.rand(500000)
temp2=zip(temp,range(len(temp)))
gc.collect()
t0=time.time()
temp2.sort()
t1=time.time()
print('elapsed time: {} seconds'.format(t1-t0))