Skip to content

Instantly share code, notes, and snippets.

@simonster
simonster / gist:8908354
Last active August 29, 2015 13:56
Benchmarks of prototypes of different Enum options
import Base.length
immutable MyOrdinalValue12
index::Uint8
pool::Vector{ASCIIString}
end
immutable MyPooledDataArray1
index::Vector{Uint8}
pool::Vector{ASCIIString}
@simonster
simonster / pathological_unique.jl
Created February 14, 2014 02:16
Strange error message
import Base: unique, hash
immutable Prehashed
hash::Uint
end
hash(x::Prehashed) = x.hash
function unique{T}(A::AbstractArray{T,2}, dim::Int)
hashes = zeros(Uint, size(A, 1))
@simonster
simonster / gist:0d397d36f1183dcd5965
Created May 23, 2014 02:14
sum_seq code comparison

With master:

julia> code_native(Base.sum_seq, (Vector{Float64}, Int, Int))
	.text
Filename: reduce.jl
Source line: 226
	push	RBP
	mov	RBP, RSP
Source line: 226
julia> (+)(A::AbstractArray{Bool},x::Bool) = A .+ x
Warning: New definition
+(AbstractArray{Bool,N},Bool) at none:1
is ambiguous with:
+(Range{T},Real) at range.jl:428.
To fix, define
+(Range{Bool},Bool)
before the new definition.
Warning: New definition
+(AbstractArray{Bool,N},Bool) at none:1
using DSP
N=1024*32;
x0=rand(N);
nn=div(N,8);
mm=div(N,16);
println("welch_pgram")
f(x0, nn, mm) = for i = 1:1000; power(Periodogramt(x0,n=nn,noverlap=mm,twosided=true,window=false)); end
f(x0, nn, mm);
@time f(x0, nn, mm);
function test(n)
a = 0
b = 0
aulpdiff = Array(Float64, n)
bulpdiff = Array(Float64, n)
for i = 1:n
x = rand()
r1 = (x*x)*(x*x)
r2 = x^4
bigres = big(x)^4
@simonster
simonster / gist:5a738201b5f3680f1e27
Last active August 29, 2015 14:04
Broadcasting mapreducedim!
using Base.Cartesian, Base.Test
using Base: check_reducdims, evaluate, @get!, mapreducedim!
function gen_mapreducedim_function(nd::Int, narrays::Int)
As = [symbol("A_"*string(i)) for i = 1:narrays]
@eval begin
local _F_
function _F_(f, op, R, $(As...))
@nextract $nd sz d->($(narrays > 1 ? :(@ncall $narrays max k->size(A_k, d)) : :(size(A_1, d))))
@nloops($nd, i, d->1:sz_d,
|-------|----------------------|------------------|------------|-------------|-------------|-----------------|-----------------|
| Row # | proc | (nsamples,ncoef) | time_julia | time_matlab | time_octave | matlab_relative | octave_relative |
| 1 | "filt SOSFilter" | (256,2) | 0.00210658 | 0.0901177 | 0.00722314 | 42.7792 | 3.42885 |
| 2 | "filt SOSFilter" | (256,4) | 0.00258519 | 0.0925957 | 0.00938837 | 35.8177 | 3.63159 |
| 3 | "filt SOSFilter" | (256,8) | 0.00363507 | 0.0985003 | 0.0133466 | 27.0972 | 3.67161 |
| 4 | "filt SOSFilter" | (256,16) | 0.00600978 | 0.109797 | 0.0212647 | 18.2697 | 3.53835 |
| 5 | "filt SOSFilter" | (1024,2) | 0.00705921 | 0.0935538 | 0.0125278 | 13.2527 | 1.77467 |
| 6 | "filt SOSFilter" | (1024,4) | 0.00867183 | 0.0966722 | 0.0192736 | 11.1478 | 2.22256
@simonster
simonster / h5dump test.jld
Last active August 29, 2015 14:05
jld immutables
HDF5 "test.jld" {
GROUP "/" {
DATASET "ComplexImmutable" {
DATATYPE "/_types/3"
DATASPACE SCALAR
DATA {
(0): {
{
1,
2.2
@simonster
simonster / gist:6195af68c6df33ca965d
Last active August 29, 2015 14:05
Magic square
function iain_magic(n::Int)
# Odd-order magic square
# http://en.wikipedia.org/wiki/Magic_square#Method_for_constructing_a_magic_square_of_odd_order
M = zeros(Int, n, n)
for I = 1:n, J = 1:n # row, column
@inbounds M[I,J] = n*((I+J-1+(n >> 1))%n) + ((I+2J-2)%n) + 1
end
return M
end