Re: [R] system.time question
Others have said what system time is, time spent in the kernel of the operating system doing things on behalf of your program. Also, switching between user and system mode can be time consuming, so the number of system calls can be important, even if each one takes little time. I don't know how the time it takes to switch is reported - is it user, system or just elapsed time. In my experience, a big system time that is a red flag and you can usually get rid of most of it. Often, reducing the system time decreases the user time as well. In many cases it is due to opening files and writing to or reading from files. E.g., define a function, f, that append a few lines to a file: f - function (filename) cat(file = filename, append = TRUE, 1:4, sep = \n) Use system.time to time it on a file in /tmp: tf - /tmp/junk.txt unlink(tf) ; system.time(for(i in 1:1)f(tf)) user system elapsed 0.752 0.216 0.967 The system time is a quarter of the user time. One way of reducing that time is to open the file once, write to it many times, then close it. (Opening a file is time consuming because it needs to be found.) unlink(tf) ; tfo - file(tf, open=w) ; system.time(for(i in 1:1)f(tfo)) ; close(tfo) user system elapsed 0.128 0.100 0.227 The system/user ratio is still high, but all the times are smaller. Here is an example of the waiting for a resource problem that Brian Ripley mentioned. I use the same function, f, but I use it on a file in my home directory, which is on a file server. (/tmp is of type 'tmpfs, which I think means it is not even on a disk.) mf - /homes/bill/junk.txt unlink(mf) ; system.time(for(i in 1:1)f(mf)) user system elapsed 1.476 1.208 41.532 The huge elapsed time it due to the kernel waiting for the file server to find /homes/bill/junk.txt, over and over again. Open it once and the times are more in line with using /tmp: unlink(mf) ; mfo - file(mf, open=w) ; system.time(for(i in 1:1)f(mfo)) ; close(mfo) user system elapsed 0.140 0.044 0.186 On Linux you can use the strace command to see what system calls R is making while your program runs. Get the process identifier from Sys.getpid() and run strace in a different window. E.g., % strace -T -p 24197 | head -100 ... lots of repetitions of the following sequence: open(/homes/bill/junk.txt, O_WRONLY|O_CREAT|O_APPEND, 0666) = 3 0.003782 fstat(3, {st_mode=S_IFREG|0664, st_size=0, ...}) = 0 0.37 mmap(NULL, 65536, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9ffae4b000 0.38 fstat(3, {st_mode=S_IFREG|0664, st_size=0, ...}) = 0 0.78 lseek(3, 0, SEEK_SET) = 0 0.11 write(3, 1, 1)= 1 0.87 write(3, \n, 1) = 1 0.88 write(3, 2, 1)= 1 0.14 write(3, \n, 1) = 1 0.10 write(3, 3, 1)= 1 0.11 write(3, \n, 1) = 1 0.10 write(3, 4, 1)= 1 0.10 write(3, \n, 1) = 1 0.09 close(3)= 0 0.001938 munmap(0x7f9ffae4b000, 65536) = 0 0.32 open(/homes/bill/junk.txt, O_WRONLY|O_CREAT|O_APPEND, 0666) = 3 0.003711 ... The times are shown in . Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Mark Leeds Sent: Saturday, October 20, 2012 9:16 AM To: R help Subject: [R] system.time question Hi : I looked at the help for system.time but I still have the following question. Can someone explain the output following output of system.time : user system elapsed 12399.681 5632.352 56935.647 Here's my take based on the fact that I was doing ps -aux | grep R off and on and the total amount of CPU minutes that got allotted before the job ended was about 5 hours and the total actual time that the job took was about 15 hours. Does elapsed = total actual time job taken ? That seems to be the case or a strange coincidence. Does user + system = CPU time from ps -aux | grep R ? That seems to be the case also or a weird coincidence. Finally, why can't the CPU get a higher percentage ? It's seems like it's always around 30% which would make sense since 5 is ~ 30% of 15 hours. Also, assuming my take above is correct, when talking about timing of algorithms, in this case, does one say the job took 5 hours or 15 hours ? I'm trying to see how fast an algorithm is compared to others and I'm not sure what the standard is. I'm on fedora 16.0 and using R 2.15. Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
[R] system.time question
Hi : I looked at the help for system.time but I still have the following question. Can someone explain the output following output of system.time : user system elapsed 12399.681 5632.352 56935.647 Here's my take based on the fact that I was doing ps -aux | grep R off and on and the total amount of CPU minutes that got allotted before the job ended was about 5 hours and the total actual time that the job took was about 15 hours. Does elapsed = total actual time job taken ? That seems to be the case or a strange coincidence. Does user + system = CPU time from ps -aux | grep R ? That seems to be the case also or a weird coincidence. Finally, why can't the CPU get a higher percentage ? It's seems like it's always around 30% which would make sense since 5 is ~ 30% of 15 hours. Also, assuming my take above is correct, when talking about timing of algorithms, in this case, does one say the job took 5 hours or 15 hours ? I'm trying to see how fast an algorithm is compared to others and I'm not sure what the standard is. I'm on fedora 16.0 and using R 2.15. Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] system.time question
You asked several questions. Elapsed: yes User + System = CPU: yes Finally: You have to look at the load and/or cpu core count. Unless you setup your code to take advantage of multiple cores, R runs on a single core. Also: Do you really need to ask that question? --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Mark Leeds marklee...@gmail.com wrote: Hi : I looked at the help for system.time but I still have the following question. Can someone explain the output following output of system.time : user system elapsed 12399.681 5632.352 56935.647 Here's my take based on the fact that I was doing ps -aux | grep R off and on and the total amount of CPU minutes that got allotted before the job ended was about 5 hours and the total actual time that the job took was about 15 hours. Does elapsed = total actual time job taken ? That seems to be the case or a strange coincidence. Does user + system = CPU time from ps -aux | grep R ? That seems to be the case also or a weird coincidence. Finally, why can't the CPU get a higher percentage ? It's seems like it's always around 30% which would make sense since 5 is ~ 30% of 15 hours. Also, assuming my take above is correct, when talking about timing of algorithms, in this case, does one say the job took 5 hours or 15 hours ? I'm trying to see how fast an algorithm is compared to others and I'm not sure what the standard is. I'm on fedora 16.0 and using R 2.15. Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org 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. __ R-help@r-project.org 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.
Re: [R] system.time question
On 20/10/2012 17:16, Mark Leeds wrote: Hi : I looked at the help for system.time but I still have the following question. Can someone explain the output following output of system.time : user system elapsed 12399.681 5632.352 56935.647 Yes, the help page can, via ?proc.time. As it says, it depends on the OS Here's my take based on the fact that I was doing ps -aux | grep R off and on and the total amount of CPU minutes that got allotted before the job ended was about 5 hours and the total actual time that the job took was about 15 hours. Does elapsed = total actual time job taken ? That seems to be the case or a strange coincidence. Does user + system = CPU time from ps -aux | grep R ? That seems to be the case also or a weird coincidence. On Fedora Linux, yes. Not in general (and what ps gives is pretty OS-specific: for example, does it include time from child processes or not -- system.time should but the OS calls used do not always do so, I find less reliably so in Fedora 16 than 14). Finally, why can't the CPU get a higher percentage ? It's seems like it's always around 30% which would make sense since 5 is ~ 30% of 15 hours. Many, many reasons. Most likely - other things are running, and some of them have a higher priority, or equal or lower priority and get lots of time slices - R the process is waiting for resources, such as memory, discs, network access Also, assuming my take above is correct, when talking about timing of algorithms, in this case, does one say the job took 5 hours or 15 hours ? I'm trying to see how fast an algorithm is compared to others and I'm not sure what the standard is. I'm on fedora 16.0 and using R 2.15. Thanks. It depends on the purpose. CRAN's check farm cares most about CPU usage: someone waiting for results cares about elapsed time. [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@r-project.org 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.
Re: [R] system.time
This is an indication of the amount of CPU resources that you are using. Elapsed time is just the number of seconds that the R process (e.g., RGUI) has been running. user time is the amount of CPU that any commands/scripts have used that you are running; this is the one that you are typically interested in. 'system' time is the amount of CPU that the operating system has used to run your script; in most cases this should be a lot less than 'user'. The reason is might be high is if you are doing a lot of I/O, or running short of physical memory and having to 'page' parts of it to disk. For a CPU intensive application, I like user:system to be at least 10:1. sprinkle proc.time() throughout your code (actually print(proc.time() so you get output when running a script or in a function). You will have to determine what the characteristics are for your script/application. On Mon, Nov 28, 2011 at 12:50 AM, Jorge I Velez jorgeivanve...@gmail.com wrote: Hi Vikram, Check ?system.time and ?proc.time. HTH, Jorge.- On Mon, Nov 28, 2011 at 12:41 AM, Vikram Bahure wrote: Dear R users. I wanted to know, how do we read the output of system.time. It would be helpful if you could let me know what are user system and elapsed. Regards Vikram [[alternative HTML version deleted]] __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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. __ R-help@r-project.org 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.
[R] system.time
Dear R users. I wanted to know, how do we read the output of system.time. It would be helpful if you could let me know what are user system and elapsed. Regards Vikram [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] system.time
Hi Vikram, Check ?system.time and ?proc.time. HTH, Jorge.- On Mon, Nov 28, 2011 at 12:41 AM, Vikram Bahure wrote: Dear R users. I wanted to know, how do we read the output of system.time. It would be helpful if you could let me know what are user system and elapsed. Regards Vikram [[alternative HTML version deleted]] __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] System.time
Wacek Kusnierczyk wrote: to contribute my few cents, here's a simple benchmarking routine, inspired by the perl module Benchmark. it allows one to benchmark an arbitrary number of expressions with an arbitrary number of replications, and provides a summary matrix with selected timings. snip it's rudimentary and not fool-proof, but might be helpful if used with care. (the nested do.call-rbind-lapply sequence can surely be simplified, but i could not resist the pattern. someone once wrote that if you need more than three (five?) levels of indentation in your code, there must be something wrong with it; presumably, he was a fortran programmer.) i have cleaned-up the code, removing the fancy nested structure. the code plus detailed documentation is available from googlecode [1], and i stop the self-marketing here. vQ [1] http://code.google.com/p/rbenchmark/ __ R-help@r-project.org 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.
Re: [R] System.time
On Wed, 2009-02-11 at 18:49 -0600, John Kerpel wrote: Hi folks! Does anyone know why I might see something like this after running system.time? system.time(svd(Mean_svd_data)) user system elapsed 0 0 0 The data set is tiny and the function returns results instantly, but shouldn't it still give me a time? Jim has already suggested why this might be the case. When I'm testing the speed of things like this (that are in and of themselves very quick) for situations where it may matter, I wrap the function call in a call to replicate(): system.time(replicate(1000, svd(Mean_svd_data))) to run it 1000 times, and that allows me to judge how quickly the function executes. HTH G Thanks, John [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% signature.asc Description: This is a digitally signed message part __ R-help@r-project.org 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.
Re: [R] System.time
Gavin Simpson wrote: On Wed, 2009-02-11 at 18:49 -0600, John Kerpel wrote: Hi folks! Does anyone know why I might see something like this after running system.time? system.time(svd(Mean_svd_data)) user system elapsed 0 0 0 The data set is tiny and the function returns results instantly, but shouldn't it still give me a time? Jim has already suggested why this might be the case. When I'm testing the speed of things like this (that are in and of themselves very quick) for situations where it may matter, I wrap the function call in a call to replicate(): system.time(replicate(1000, svd(Mean_svd_data))) to run it 1000 times, and that allows me to judge how quickly the function executes. the timethese function in the Benchmark module in perl takes as arguments not only the code to be run, but also the number of replications to be performed. it will complain if the code runs too fast, i.e., if the measured time is too short and is not a reliable estimate. it might be a good idea to have a similar functionality in r (maybe it's already there?), which would basically wrap over system.time and issue a warning when reliable measurement canno tbe made. vQ __ R-help@r-project.org 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.
Re: [R] System.time
On Thu, Feb 12, 2009 at 4:28 AM, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: When I'm testing the speed of things like this (that are in and of themselves very quick) for situations where it may matter, I wrap the function call in a call to replicate(): system.time(replicate(1000, svd(Mean_svd_data))) to run it 1000 times, and that allows me to judge how quickly the function executes. I do the same, but with a small twist: system.time(replicate(1000, {svd(Mean_svd_data); 0} )) This allows the values of svd(...) to be garbage collected. If you don't do this and the output of the timed code is large, you may allocate large amounts of memory (which may influence your timing results) or run out of memory (which will also influence your timing results :-) ), -s __ R-help@r-project.org 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.
Re: [R] System.time
On Thu, Feb 12, 2009 at 8:42 AM, Stavros Macrakis macra...@alum.mit.edu wrote: On Thu, Feb 12, 2009 at 4:28 AM, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: When I'm testing the speed of things like this (that are in and of themselves very quick) for situations where it may matter, I wrap the function call in a call to replicate(): system.time(replicate(1000, svd(Mean_svd_data))) to run it 1000 times, and that allows me to judge how quickly the function executes. I do the same, but with a small twist: system.time(replicate(1000, {svd(Mean_svd_data); 0} )) You could also do r_ply(1000, svd(Mean_svd_data)) which has the same effect - the results are discarded after each evaluation (as opposed to raply, rlply and rdply where they are kept and returned in various formats) Hadley -- http://had.co.nz/ __ R-help@r-project.org 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.
Re: [R] System.time
On Thu, 2009-02-12 at 09:42 -0500, Stavros Macrakis wrote: On Thu, Feb 12, 2009 at 4:28 AM, Gavin Simpson gavin.simp...@ucl.ac.uk wrote: When I'm testing the speed of things like this (that are in and of themselves very quick) for situations where it may matter, I wrap the function call in a call to replicate(): system.time(replicate(1000, svd(Mean_svd_data))) to run it 1000 times, and that allows me to judge how quickly the function executes. I do the same, but with a small twist: system.time(replicate(1000, {svd(Mean_svd_data); 0} )) This allows the values of svd(...) to be garbage collected. If you don't do this and the output of the timed code is large, you may allocate large amounts of memory (which may influence your timing results) or run out of memory (which will also influence your timing results :-) ), Thanks for that tip Stavros. I hadn't realised that. G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@r-project.org 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.
[R] System.time
Hi folks! Does anyone know why I might see something like this after running system.time? system.time(svd(Mean_svd_data)) user system elapsed 0 0 0 The data set is tiny and the function returns results instantly, but shouldn't it still give me a time? Thanks, John [[alternative HTML version deleted]] __ R-help@r-project.org 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.
Re: [R] System.time
It depends on the granularity that the operating system is recording that time; on some systems the minimum might be 0.01 seconds. If it is that short, why worry about it. There is nothing unusual about the result. On Wed, Feb 11, 2009 at 7:49 PM, John Kerpel john.ker...@gmail.com wrote: Hi folks! Does anyone know why I might see something like this after running system.time? system.time(svd(Mean_svd_data)) user system elapsed 0 0 0 The data set is tiny and the function returns results instantly, but shouldn't it still give me a time? Thanks, John [[alternative HTML version deleted]] __ R-help@r-project.org 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 Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@r-project.org 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.
[R] system.time()
I thought system.time() would return three numbers, the first two adding up to the third one. However, this does not hold in my system running Windows Vista Home Premium, with an Intel Core 2 Duo processor. For example, using the code in the help for system.time() (with one zero added), I got: system.time(for(i in 1:100) mad(runif(1))) user system elapsed 0.270.000.25 Is this a problem with my system clock or am I missing something? Thanks. FS __ R-help@r-project.org 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.
Re: [R] system.time()
The first two numbers will not add up to the third. The third is the elapsed time (wall clock) and the first two are CPU time; even though they have what appears to be the same units (seconds), they can not really be compared. If, for example, was script was taking input from the console, you will get something like this: system.time({ + x - scan() + }) 1: 12345 2: Read 1 item user system elapsed 0.000.067.34 Here it took 7 seconds for me to enter the data, on only 0.06 seconds of CPU (all system time -- probably the 'read' function). The differences in your numbers were the CPU is larger than the elapsed might be due to the resolution of the time since it was a very short time, or if you are running an a multiprocessor, and the application can run multithreaded, you will see the same thing happen -- e.g., if you have 2 CPU and the application could multithread and it was CPU bound, then in 10 seconds of elapsed time you might see 20 seconds of CPU time being used. HTH On Sat, Apr 26, 2008 at 8:32 PM, Fernando Saldanha [EMAIL PROTECTED] wrote: I thought system.time() would return three numbers, the first two adding up to the third one. However, this does not hold in my system running Windows Vista Home Premium, with an Intel Core 2 Duo processor. For example, using the code in the help for system.time() (with one zero added), I got: system.time(for(i in 1:100) mad(runif(1))) user system elapsed 0.270.000.25 Is this a problem with my system clock or am I missing something? Thanks. FS __ R-help@r-project.org 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 Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@r-project.org 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.
[R] system.time gives error when = is used for assignment (R-2.6.0)
Hello list, I found that system.time works correctly when I used - to assign a value to a variable but when I happened to use = instead of -, R gave an error message: Error in system.time(your argument here...). It happened with a few functions I tried. Is this a bug or is there any circumstances that = cannot be used for assignment? Here is a real simple example. fn1 - function(x) x+1 r1 - system.time(res1=fn1(2)) r2 - system.time(res2 - fn1(2)) Thank you. Kyeongmi University of Memphis __ R-help@r-project.org 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.
Re: [R] system.time gives error when = is used for assignment (R-2.6.0)
On Thu, 10 Apr 2008, Kyeongmi Cheon wrote: Hello list, I found that system.time works correctly when I used - to assign a value to a variable but when I happened to use = instead of -, R gave an error message: Error in system.time(your argument here...). It happened with a few functions I tried. Is this a bug or is there any circumstances that = cannot be used for assignment? Here is a real simple example. The latter. Although some might say the former, too. And some will no doubt flame me for even saying that. The former, I mean. Arguments in calls to function (aka arglists) can be in the name=object form. So, (function(x,y) x-y)(y=3,x=2) evaluates to -1, while (function(x,y) x-y)( 3, 2 ) evaluates to 1, as does (function(x,y) x-y)(me - 3, you - 2) but (function(x,y) x-y)(me = 3, you = 2) gives an error when it tries to match 'me' or 'you' to an argument but fails to find one. Hint: often system.time( { result.of.long.compute - foo(my.args) } ) is what you want. HTH, Chuck p.s. As an exercise for the reader, why does (function(x,y) x-y)({me = 3},{you = 2}) not return an error? fn1 - function(x) x+1 r1 - system.time(res1=fn1(2)) r2 - system.time(res2 - fn1(2)) Thank you. Kyeongmi University of Memphis __ R-help@r-project.org 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. Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 __ R-help@r-project.org 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.
Re: [R] system.time gives error when = is used for assignment(R-2.6.0)
Kyeongmi asks: Hello list, I found that system.time works correctly when I used - to assign a value to a variable but when I happened to use = instead of -, R gave an error message: Error in system.time(your argument here...). It happened with a few functions I tried. Is this a bug or is there any circumstances that = cannot be used for assignment? Here is a real simple example. fn1 - function(x) x+1 r1 - system.time(res1=fn1(2)) r2 - system.time(res2 - fn1(2)) No, it is not a bug. It is a syntactic trap that using '=' for assignment will lure you into. This is one reason why I suggest you just do not do it. Think about it. If the function system.time() had an argument res1, how would you call it giving a value for this argument? As you have done. So it has nothing to do with the function system.time iteslf, it applies generally. Ways around it include r1 - system.time((res1 = fn1(2))) ## if you must! r1 - system.time({res1 = fn1(2)}) ## ditto r1 - system.time(res1 - fn1(2)) ## as you discovered. Bill Venables. __ R-help@r-project.org 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.
Re: [R] system.time behavior using source
You need to print the result of an expression if used from source(). Autoprinting only occurs at the top level. E.g. % cat systest.R print(system.time(for(i in 1:100) mad(runif(1000 source(systest.R) user system elapsed 0.140.000.20 On Mon, 17 Sep 2007, Leeds, Mark (IED) wrote: If I type the line below at my Rprompt, it works fine. But, if I put the same line in a file called systest.R and then source that file at the prompt, nothing happens. IF I use R CMD BATCH, it also works in that the system.time info is the .Rout file. Is there something I can do so that this line also works when I source it ? Thanks. My information is below. system.time(for(i in 1:100) mad(runif(1000))) My information : R version 2.5.0 (2007-04-23) i686-pc-linux-gnu locale: C attached base packages: [1] datasets utils stats graphics grDevices methods base other attached packages: lattice filehash reshape zoochron MASS 0.15-81.0 0.7.4 1.3-1 2.3-11 7.2-33 This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@r-project.org 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. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@r-project.org 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.
Re: [R] system.time behavior using source
Try this: source(myfile.R, echo = TRUE) On 9/17/07, Leeds, Mark (IED) [EMAIL PROTECTED] wrote: If I type the line below at my Rprompt, it works fine. But, if I put the same line in a file called systest.R and then source that file at the prompt, nothing happens. IF I use R CMD BATCH, it also works in that the system.time info is the .Rout file. Is there something I can do so that this line also works when I source it ? Thanks. My information is below. system.time(for(i in 1:100) mad(runif(1000))) My information : R version 2.5.0 (2007-04-23) i686-pc-linux-gnu locale: C attached base packages: [1] datasets utils stats graphics grDevices methods base other attached packages: lattice filehash reshape zoochron MASS 0.15-81.0 0.7.4 1.3-1 2.3-11 7.2-33 This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@r-project.org 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. __ R-help@r-project.org 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.