From urbanek at research.att.com  Fri Feb 11 20:26:37 2005
From: urbanek at research.att.com (Simon Urbanek)
Date: Fri Feb 11 20:26:43 2005
Subject: [Rd] Re: [R-SIG-Mac] Bug running pbinom() in R-GUI?
In-Reply-To: <BE3168A8.4737%gwgilc@wm.edu>
References: <BE3168A8.4737%gwgilc@wm.edu>
Message-ID: <D8FE15E0-7C62-11D9-B113-000D93AE1C66@research.att.com>

On Feb 10, 2005, at 7:38 PM, George W. Gilchrist wrote:

> Today I was running a graduate level stats lab using R and we 
> encountered a
> major problem while using the current build of the Cocoa GUI:
>
>> From the GUI:
>> system.time(pbinom(80, 1e5, 806/1e6))
> [1] 14.37  4.94 30.29  0.00  0.00
>>
>
>> From the command line on the same machine:
>> system.time(pbinom(80, 1e5, 806/1e6))
> [1] 0.02 0.00 0.02 0.00 0.00
>>
>
> I've tried the CRAN version and the latest build of the R-GUI and both
> deliver the same terrible performance. It seems as if this only occurs 
> for
> certain arguments, but we saw this on ~15 different machines today. 
> And it
> seems to only be when running the Cocoa GUI. No problems at all with 
> this
> under Windoze. Any ideas?

The cause is pbeta_raw calling (indirectly via R_CheckUserInterrupt) 
R_ProcessEvents for every iteration - and for small p the number of 
iterations goes really high (e.g. for the case above n=99919). 
R_ProcessEvents is not a cheap operation, because it enters system 
event loop and processes any pending events. A quick fix could be the 
following patch, which checks for break only after several iterations 
(including the first one, just in case this is part of a sequence that 
may need user interaction).

Index: src/nmath/pbeta.c
===================================================================
--- src/nmath/pbeta.c   (revision 33148)
+++ src/nmath/pbeta.c   (working copy)
@@ -139,7 +139,8 @@
             for(i= 1; i <= n; i++) {
  #ifndef MATHLIB_STANDALONE
                 /* for now, at least allow this:*/
-               R_CheckUserInterrupt();
+               if ((i&1023)==1)
+                       R_CheckUserInterrupt();
  #endif
                 if (p1 <= 1 && term / eps <= finsum)
                     break;

after this patch has been applied I get in the GUI:

 > system.time(pbinom(80, 1e5, 806/1e6))
[1] 0.02 0.00 0.08 0.00 0.00

Cheers,
Simon


[Rd] Re: [R-SIG-Mac] Bug running pbinom() in R-GUI?
Simon Urbanek