to print system.time always

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

to print system.time always

mrzung
Hi all;

I want to print system.time whenever I execute any command.

It takes too much time to type "system.time()" function to all command.

is there any solution on it?

And,

apply(matrix,1,cumsum) command is too slow to some large matrix.

is there any function like rowCumSums ?

thank u!
Reply | Threaded
Open this post in threaded view
|

Re: to print system.time always

jholtman
Here is a faster solution to your 'apply'; use 'sapply' instead:

> str(x)
 num [1:1000000, 1:30] 0.0346 0.4551 0.66 0.8528 0.5494 ...

> system.time(y <- apply(x, 1, cumsum))
   user  system elapsed
  13.24    0.61   14.02
> system.time(ys <- sapply(1:col, function(a) cumsum(x[,a])))
   user  system elapsed
   1.40    0.14    1.59


On Sat, Nov 3, 2012 at 11:52 AM, mrzung <[hidden email]> wrote:

> Hi all;
>
> I want to print system.time whenever I execute any command.
>
> It takes too much time to type "system.time()" function to all command.
>
> is there any solution on it?
>
> And,
>
> apply(matrix,1,cumsum) command is too slow to some large matrix.
>
> is there any function like rowCumSums ?
>
> thank u!
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/to-print-system-time-always-tp4648314.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: to print system.time always

Uwe Ligges-3
In reply to this post by mrzung


On 03.11.2012 16:52, mrzung wrote:
> Hi all;
>
> I want to print system.time whenever I execute any command.
>
> It takes too much time to type "system.time()" function to all command.
>
> is there any solution on it?

See ?Rprof on how to profile your code.

>
> And,
>
> apply(matrix,1,cumsum) command is too slow to some large matrix.
>
> is there any function like rowCumSums ?

You had:

result1 <- apply(matrix,1,cumsum)


This is only "slow", if you have lots of rows. Now "think in matrices"
how to to that:

b <- sapply(1:ncol(matrix), function(i) c(rep(1, i), rep(0, ncol(matrix)-i)
result2 <- t(x %*% b)

This is roughly 10 times faster on a 1000000 x 10 matrix.

Check the results:
all.equal(result1, result2)


Uwe Ligges






> thank u!
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/to-print-system-time-always-tp4648314.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: to print system.time always

Uwe Ligges-3
In reply to this post by jholtman


On 03.11.2012 19:42, jim holtman wrote:

> Here is a faster solution to your 'apply'; use 'sapply' instead:
>
>> str(x)
>   num [1:1000000, 1:30] 0.0346 0.4551 0.66 0.8528 0.5494 ...
>
>> system.time(y <- apply(x, 1, cumsum))
>     user  system elapsed
>    13.24    0.61   14.02
>> system.time(ys <- sapply(1:col, function(a) cumsum(x[,a])))
>     user  system elapsed
>     1.40    0.14    1.59


Which solves another problem (cumsum of cols rather than rows). Applying
it on rows won't be much faster.

Uwe Ligges


>
> On Sat, Nov 3, 2012 at 11:52 AM, mrzung <[hidden email]> wrote:
>> Hi all;
>>
>> I want to print system.time whenever I execute any command.
>>
>> It takes too much time to type "system.time()" function to all command.
>>
>> is there any solution on it?
>>
>> And,
>>
>> apply(matrix,1,cumsum) command is too slow to some large matrix.
>>
>> is there any function like rowCumSums ?
>>
>> thank u!
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/to-print-system-time-always-tp4648314.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: to print system.time always

jholtman
In reply to this post by mrzung
I use notepad++ on Windows, so it is easy to add a "hotkey" that will
surround a block of code that you want to execute with:

system.time({......code to run......})

Usually you don't want it around each statement.  I use the following
function to have it print out CPU and memory usage at various
locations in my script since it allows me to see the progress and
where time might be going:

"my.stats" <-
local({
    # local variables to hold the last times
    # first two variables are the elasped and CPU times from the last report

    Level    <- 1
    MaxLevel <- 30
    Stack    <- matrix(0, ncol=2, nrow=MaxLevel)
    function(text = "stats", reset=FALSE, oper="")
    {
        procTime <- proc.time()[1:3]    # get current metrics
        if (reset){ # setup to mark timing from this point
            Level <<- 1     # reset the Level
            Stack[Level, ] <<- c(procTime[3], procTime[1] + procTime[2])
        }
        if (oper == "push"){
            if (Level < MaxLevel) Level <<- Level + 1
            Stack[Level, ] <<- c(procTime[3], procTime[1] + procTime[2])
        }
        .caller <- sys.calls()
        if (length(.caller) == 1) .caller <- "Rgui"
        else .caller <- as.character(.caller[[length(.caller) - 1]])[1]
        cat(sprintf("%s (%d) - %s : %s <%.1f %.1f> %.1f : %.1fMB\n",
            text,
            Level,
            .caller,
            format(Sys.time(), format="%H:%M:%S"),
            procTime[1] + procTime[2] - Stack[Level, 2],
            procTime[3] - Stack[Level, 1],
            procTime[3],
            memory.size()))
        if ((oper == "pop") && (Level > 1)) Level <<- Level - 1
        else if (oper == "reset") Level <<- 1
        invisible(flush.console())  # force a write to the console
    }
})

It produces output like this:

> my.stats('start')
start (1) - Rgui : 14:53:16 <39.2 597822.1> 597822.1 : 1213.8MB
> system.time(for(i in 1:col) ym[, i] <- cumsum(x[,i]))
   user  system elapsed
   1.77    0.01    1.80
> my.stats('done')
done (1) - Rgui : 14:53:23 <41.0 597828.6> 597828.6 : 1213.8MB


This says that between 'start' and 'done',   1.8 CPU seconds were used
(41.0 - 39.2) which is what syste.time was reporting.




On Sat, Nov 3, 2012 at 11:52 AM, mrzung <[hidden email]> wrote:

> Hi all;
>
> I want to print system.time whenever I execute any command.
>
> It takes too much time to type "system.time()" function to all command.
>
> is there any solution on it?
>
> And,
>
> apply(matrix,1,cumsum) command is too slow to some large matrix.
>
> is there any function like rowCumSums ?
>
> thank u!
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/to-print-system-time-always-tp4648314.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.