Skip to content
Prev 22941 / 63421 Next

overhead of function calls

On Sat, 18 Nov 2006, Tamas K Papp wrote:

            
Not compared with all but the very simplest computations: the overhead is 
of the order of a few microseconds.  If it were otherwise, R would not to 
a large extent be implemented by function wrappers round .Internal 
calls (rather than .Primitives).
Note that subsequent calls change quite a lot:
[1] 12.989  0.150 13.215  0.000  0.000
[1] 8.751 0.105 8.923 0.000 0.000
[1] 7.993 0.175 8.191 0.000 0.000
[1] 7.016 0.058 7.074 0.000 0.000
[1] 7.748 0.181 7.932 0.000 0.000
[1] 8.289 0.074 8.371 0.000 0.000

Several things are going on here, but most likely the most important is 
that the memory manager is being tuned.  On my (64-bit) system:
used (Mb) gc trigger (Mb) max used (Mb)
Ncells  224202 12.0     407500 21.8   350000 18.7
Vcells 1115457  8.6    1519644 11.6  1419269 10.9
used (Mb) gc trigger  (Mb) max used  (Mb)
Ncells  224206 12.0    2156470 115.2  2665684 142.4
Vcells 2115455 16.2    7060137  53.9  8164087  62.3

Note the factor 5x differences.

It is better to profile here, so if I profile
I get
     self.time self.pct total.time total.pct
"f"      2.18     62.6       2.34      67.2
"g"      1.14     32.8       3.48     100.0
"+"      0.16      4.6       0.16       4.6


The extra call to g is taking about 1 usec (as I expected).  Note that 
lazy evaluation means (I think) that f is being charged for the evaluation 
of the argument, not g.
No (and calls to .Call carry less, as there is no duplication on call and 
return).