MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Two speed challenges

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47418] Re: Two speed challenges
  • From: koopman at sfu.ca (Ray Koopman)
  • Date: Sat, 10 Apr 2004 02:01:23 -0400 (EDT)
  • References: <c4u1us$9a3$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"Gareth J. Russell" <gjr2008 at columbia.edu> wrote in message 
news:<c4u1us$9a3$1 at smc.vnet.net>...
> 
> I would appreciate any advice on the following two pieces of code,
> which are the rate-limiting factors and are as fast as I can make them.
> Can anyone do better?
> 
> Code 1
> 
> This simply randomizes a vector (in case it matters,
> typical input is a 500-element vector of Reals).
> 
> listRandomize = Compile[{{l, _Real, 1}},
>     Sort[Table[{Random[], l[[i]]}, {i, 1, Length[l]}]][[All, 2]]]

It's usually faster to avoid named index variables. Try sorting
Map[{Random[],#}&,l]  or  Transpose[{Table[Random[],{Length[l]}],l}].

Also, don't bet the family farm on Random. I have found it can be quite
nonrandom, even in applications that use only the order properties of its
output. See my 16 Jun 2003 post "Warning -- another Random[] failure".
For confirmation that there is a problem and for a workaround, see Daniel
Lichtblau's 5 May 2000 post "Re: [mg23340] Random Geometric (long)".
> 
> 
> Code 2
> 
> This computes the only part of Moran's I that must change when the
> ordering of elements in the vector z changes. It calculates the sum
> over all i and j of z_i*z_j*w_ij, where w is (obviously) a square matrix
> 
> moranIPart1 = Compile[{{z, _Real, 1}, {w, _Real, 2}},
>               Apply[Plus, Apply[Plus, Outer[Times, z, z]*w]]]

Use the built-in matrix-multiply operator.  z.w.z  should be much faster.


  • Prev by Date: Adding hyperlinks to help browser files in function::usage
  • Next by Date: Differential Equations
  • Previous by thread: Two speed challenges
  • Next by thread: Constraint violation by FindMinimum w/ 2nd-start arg(?)