MathGroup Archive 2003

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

Search the Archive

Speed Tips

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39601] Speed Tips
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Tue, 25 Feb 2003 02:57:28 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

I have a tip (or a few tips) that can slightly improve the speed of
programs.  I figured this is worth mentioning because I notice that even
some experts don't take advantage of this.  

First[expr]  is faster than   expr[[1]]
Last[expr]  is faster than    expr[[-1]]
Rest[expr]  is faster than   Drop[expr,1]
Head[expr]  is faster than   expr[[0]]

Apparently  First, Last, Rest, Head  are more efficient because they don't
need to see which parts to return, and I am sure these functions were
created for this very reason.  In general one should always use the form
that requires the minimum work for the kernel.  So for example:

Don't use   Table[expr,{i,imax}]  when  Table[expr,{imax}]  will work.
Don't use   Do[expr,{i,imax}]      when  Do[expr,{imax}]  will work.


------
Regards,
Ted Ersek
Download Mathematica tips, tricks from
http://www.verbeia.com/mathematica/tips/Tricks.html



  • Prev by Date: Re: plot help
  • Next by Date: Re: Cursors in graphics
  • Previous by thread: Re: Re: FindMinimum
  • Next by thread: Re: Speed Tips