Elapsed CPU time in Julia?

1,588 views
Skip to first unread message

Lytu

unread,
Feb 3, 2016, 3:28:28 PM2/3/16
to julia-users
Hello Julia users,

Can someone tell me what's the equivalent of matlab elapsed cputime in Julia

For example i Matlab, we can do this:
   t = cputime;
   x=4;
   iter = 1; 
   z = ones(1,4);
   y=x*2*z;
e = cputime-t

But in Julia i don't seem to find how to do this. I thought i can use 
   t=time()
   x=4;
   iter = 1; 
   z = ones(1,4);
   y=x*2*z;
   e=time()-t

But time() in Julia is not the elapsed CPU time, it's a wall clock time.

Can someone help me?

Thank you

Christopher Alexander

unread,
Feb 3, 2016, 3:55:44 PM2/3/16
to julia-users
Try doing something like:

tic()
# my code
toc()

Milan Bouchet-Valat

unread,
Feb 3, 2016, 4:05:46 PM2/3/16
to julia...@googlegroups.com
Le mercredi 03 février 2016 à 12:55 -0800, Christopher Alexander a
écrit :
> Try doing something like:
>
> tic()
> # my code
> toc()
Be careful with tic() and toc() in Julia. In most cases, when
benchmarking, you should wrap your code in a function to make sure it
gets specialized on the argument types, instead of running it from the
REPL. So in general it's better to do:
@time myfun(arg1, arg2, ...)

See http://docs.julialang.org/en/latest/manual/performance-tips/#measur
e-performance-with-time-and-pay-attention-to-memory-allocation


Regards

Stefan Karpinski

unread,
Feb 3, 2016, 4:58:09 PM2/3/16
to Julia Users
You may want to check out Benchmarks.jl, which goes to significant lengths to do benchmarking correctly.

Tim Holy

unread,
Feb 3, 2016, 5:01:50 PM2/3/16
to julia...@googlegroups.com
I'm not sure there's currently a way to get CPU time directly, although it's
just a couple ccalls so it wouldn't be hard to implement.

Out of curiosity, why do you want it? CPU time is often quite misleading; for
a multithreaded operation, it sums all the time used by all the cores.
Consequently, taking it at face value can lead you to think that your fast,
multithreaded calculation takes _longer_ than a slower single-threaded
implementation. I've encountered quite a few Matlab users who think repmat is
faster than bsxfun simply because the profiler told them that was true.

Following a dim memory, I see that I seem to have found this important enough
that I memorialized it in my startup.m file, for which the entire contents are:

if exist('distinguishable_colors','file')
set(0,'DefaultAxesColorOrder',distinguishable_colors(20));
end
% Put the profiler into a mode where it uses elapsed time rather than CPU
% time (which is messed up for multicore machines)
profile -timer real

Best,
--Tim

Lytu

unread,
Feb 3, 2016, 6:17:12 PM2/3/16
to julia-users
Thank you for your replies, but i can not use tic() #my code toc() because all i want is a CPU time, and i can't use @time myFunction because it's in the middle of the function, see the matlab code below. I do t0=cputime; on the first line of my function and t = [t cputime-t0]; on the sixth line from the bottom.

Sadly i can not do the same thing in Julia, although here https://github.com/schmrlng/CPUTime.jl they are many macros concerning CPU time.

function [H,e,t]=func(A,H,maxiter,timelimit)

    t0    = cputime;
    
    [n,r] = size(H);
    e = []; t = [];
    iter = 1; x = zeros(1,4);
    if nargin < 3, maxiter   = 100; end
    if nargin < 4, timelimit = 60;  end    
    HHt = H*H';
    scaling = sum(sum(A.*HHt))/sum(sum(HHt.*HHt));
    H   = sqrt(scaling)*H; 
    while iter <= maxiter && cputime-t0 <= timelimit
        R  = A-H*H';   
        for k=1:r
            R  = R+H(:,k)*H(:,k)';
            dR = diag(R);         
            HtH = H(:,k)'*H(:,k);            
            for i=1:n               
                HtH = HtH-H(i,k)^2 ;
                a   = HtH-dR(i);
                b   = -(H(:,k)'*R(:,i) - H(i,k)*dR(i));
                Delta = 4*a^3+27*(b)^2;
                d = 0.5*(-b+sqrt(Delta/27));
                if Delta <= 0 
                    r3   = 2*(abs(d)^(1/3)); 
                    th3  = angle(d)/3;       
                    x(2) = r3*cos(th3);
                    x(3) = r3*cos(th3+(2*pi/3));
                    x(4) = r3*cos(th3+(4*pi/3));
                    x       = x(x>=0);
                    [~,ind] = min(x.^4/4+a*x.^2/2 + b*x);
                    H(i,k)  = x(ind);
                    HtH     = HtH + H(i,k)^2;
                else
                    z       = sign(d)*(abs(d))^(1/3);
                    val     = z-a/(3*z);
                    if val.^4/4+a*val.^2/2 + b*val<0 && val >= 0
                        HtH    = HtH + val^2;
                        H(i,k) = val;
                    else
                        H(i,k) = 0;
                    end
                end

            end
            R=R-H(:,k)*H(:,k)';
        end
        if nargout >=2

            t = [t cputime-t0];

            e = [e 0.5*norm(A-H*H','fro')^2];
        end
        iter = iter + 1;
    end
end

Lutfullah Tomak

unread,
Feb 3, 2016, 6:46:07 PM2/3/16
to julia-users
Hi
You may look what @time/@elapsed uses for timings

macroexpand(:(@time begin; 2+2; end))
or
macroexpand(:(@elapsed begin; 2+2; end))

From these, It looks like you can use time_ns()

Kevin Squire

unread,
Feb 3, 2016, 7:52:22 PM2/3/16
to julia...@googlegroups.com
Benchmarks is great!  JMW, any chance you can register it as an official package (and deprecate Benchmark.jl).

(I guess I could file an issue.)

Cheers,
   Kevin 

Lytu

unread,
Feb 4, 2016, 5:56:32 AM2/4/16
to julia-users
The problem is that it is not possible for me to add Benchmarks.jl package when i do Pkg,add("Benchmarks"), i have something like "unknown package Benchmarks".

Patrick Kofod Mogensen

unread,
Feb 4, 2016, 7:33:43 AM2/4/16
to julia-users
That is because it is not registered, you have to use Pkg.clone("https://github.com/johnmyleswhite/Benchmarks.jl")

Ritchie Lee

unread,
Feb 4, 2016, 12:34:05 PM2/4/16
to julia-users
You may want to take a look at the CPUTime package.
https://github.com/schmrlng/CPUTime.jl
Message has been deleted

Lytu

unread,
Feb 4, 2016, 3:49:59 PM2/4/16
to julia-users
When i do :
using Benchmarks
@benchmark sin(2.0)

I have an error like : ERROR: @benchmark not defined

Has anyone been able to successfully use Benchmarks.jl ?

Stefan Karpinski

unread,
Feb 4, 2016, 3:51:54 PM2/4/16
to Julia Users
Works ok here. Are you using an old version or something? Or maybe you have a file called benchmarks.jl that's getting loaded instead of the package?

On Thu, Feb 4, 2016 at 3:49 PM, Lytu <lyan...@gmail.com> wrote:
When i do :
using Benchmarks
@benchmark sin(2.0)

I have an error like : 

Has anyone been able to successfully use Benchmarks.jl ?


On Wednesday, 3 February 2016 22:58:09 UTC+1, Stefan Karpinski wrote:
Reply all
Reply to author
Forward
0 new messages