Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Programing tips: What is faster?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113040] Programing tips: What is faster?
  • From: Themis Matsoukas <tmatsoukas at me.com>
  • Date: Tue, 12 Oct 2010 04:24:01 -0400 (EDT)

I recall reading that Mathematica saves its previous calculations so that it does not compute something unnecessarily if it has been computed already. Should I take this to mean that the following two examples would run equally fast?

EXAMPLE 1
=======
f[MyList_] := Total[MyList]
a = f[MyList]
b = f[MyList]

EXAMPLE 2
=======
f[MyList_] := Total[MyList]
z = f[MyList]
a = z
b = z

I have a lengthy simulation and I'm looking for ways to speed it up. The examples here are just a demonstration. In my actual calculation, f is a more complicated function, MyList changes during the course of the calculation,  and f is called several times, not necessarily in consecutive lines as shown here. Option #1 is preferable to me because it does not require me to check whether MyList has changed between calls, as I would have to do under #2. But if its slower because of the two calls to f, I'd invest the time to code my notebook according to #2. 

Thanks

Themis


  • Prev by Date: Calculating Multiple Integrals on Mathematica (Electric fields)
  • Next by Date: Re: List to infix
  • Previous by thread: Calculating Multiple Integrals on Mathematica (Electric fields)
  • Next by thread: Re: Programing tips: What is faster?