function times = benchmark(N, f_min, x, sr)
% benchmark(N, f_min, x, sr)

    if nargin < 1 || isempty(N)
        N = 10;
    end
    if nargin < 2 || isempty(f_min)
        f_min = 16.35;
    end
    if nargin < 4 || isempty(x)
        [x,sr] = wavread('fugue.wav');
    end
    K = cqkern(sr,12,f_min);
    minval = 0.15;
    sK = sparseK(K, minval);
    times = struct('cq',[],'cqkern',[],'sparse',[],
                   'ecq',[],'vecq',[],'secq',[],'svecq',[]);

    tic
    printf("cq    "); times.cq = statistics(bench(@() Xcq = cq(x,sr), N));
    printf("cqkern"); times.cqkern = statistics(bench(@() K = cqkern(sr,12,f_min), N));
    printf("sparse"); times.sparse = statistics(bench(@() sK = sparseK(K, minval), N));
    printf("ecq   "); times.ecq = statistics(bench(@() Xcq = ecq(x,K), N));
    printf("vecq  "); times.vecq = statistics(bench(@() Xcq = vecq(x,K), N));
    printf("secq  "); times.secq = statistics(bench(@() Xcq = ecq(x,sK), N));
    printf("svecq "); times.svecq = statistics(bench(@() Xcq = vecq(x,sK), N));
    toc


    save benchmark.sav times

function [t, ret] = bench(func, N)
    t = zeros(N, 3);
    for i=1:N
        printf("."); fflush(stdout);
        [w,u,s] = cputime;
        t1 = [w,u,s];

        ret = func();

        [w,u,s] = cputime;
        t(i,:) = [w,u,s] - t1;
    end
    printf("\n"); fflush(stdout);

function sK = sparseK(K, minval)
    sK = K;
    sK(abs(K) < minval) = 0;
    sK = sparse(sK);
