Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1990
*January
*February
*March
*April
*May
*June
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1990

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

Search the Archive

Mathematica speedup technique

  • To: mathgroup at yoda.ncsa.uiuc.edu
  • Subject: Mathematica speedup technique
  • From: uunet!cello.hpl.hp.com!jacobson
  • Date: Thu, 22 Mar 90 13:49:13 PST

I am presently working on a project where we needed to assign values
to MANY complicated expressions, as in

	FinishTime[packetToNet] = .1243

	FinishTime[PE[PI[diskread],1]] = .345

This can get real slow.  

	In[1]:= Timing[Do[FinishTime[PE[PI[diskread],i]] = i+1,{i,2000}]]

	Out[1]= {152.4 Second, Null}

	In[2]= Timing[FinishTime[PE[PI[diskread],500]]]

	Out[2]= {0.01 Second, 501}

I found that I could hash the expression to a new symbol and then
use it as the tag in a TagSet.  This speeded things up a lot.

I have coded this up in a package that defined a function "Wrap" to wrap
around the expressions.  The heart of the function is this definition:

   hashify[name_] := ToExpression[StringJoin["HASH$",ToString[Hash[name]]]]

The whole package appears at the end.

    In[1]:= <<wrap.m

    In[2]:= Timing[Do[Wrap[FinishTime[PE[PI[diskread],i]]] = i+1,{i,2000}]]

    Out[2]= {16.16 Second, Null}

    In[3]:= Timing[Wrap[FinishTime[PE[PI[diskread],500]]]]

    Out[3]= {0., 501}

WARNING: When I was preparing this, I was taking wrap, spelled in
lower case, from a larger program.  It was not in a package and was
simply entered one line at a time, down through the definition of
wrapset.  The above example did not work.  There must be a bug lurking
somewhere.
-----------------------------
(**********************************************************************
This "Wrap" package is experimental and HP shall have no obligation to
maintain or support it.  HP makes no express or implied warranty of
any kind with respect to this software, and HP shall not be liable for
any direct, indirect, special, incidental or consequential damages
(whether based on contract, tort or any other legal theory) arising in
any way from use of the software.
***********************************************************************)

BeginPackage["Wrap`"]

Wrap::usage = "Wrap[arg] = expr effectively assigns expr to wrap[arg],
but does so using TagSet with the tag being a hash value.  Wrap[exp]
retrieves that value.  Wrap[arg] := expr and Wrap[arg] =. also work.
Feature(?): Wrap[foo] returns an ugly expression if it has never been
defined.  Perhaps it ought to not evaluate, but we are doing this for
speed."

Begin["`Private`"]

hashify[name_] := ToExpression[StringJoin["HASH$",ToString[Hash[name]]]]

SetAttributes[wp,HoldRest]

Wrap/:Set[Literal[Wrap[arg_]],val_] := wrapset[hashify[arg],arg,val] 

wrapset[h_,arg_,val_] := h/: wp[h,arg] = val

Wrap[arg_] := wp[hashify[arg],arg]

Wrap/:Unset[Literal[Wrap[arg_]]] := wrapunset[hashify[arg],arg]

wrapunset[h_,arg_] := h/:wp[h,arg]=.

SetAttributes[wrapsetdelayed,HoldRest]

Wrap/:SetDelayed[Literal[Wrap[arg_]],val_] := wrapsetdelayed[hashify[arg],arg,val] 

wrapsetdelayed[h_,arg_,val_] := h/: wp[h,arg] = val

End[]

EndPackage[]
-----------------------------------------



  • Prev by Date: Re: N
  • Next by Date: evaluation
  • Previous by thread: D and Derivative - help please
  • Next by thread: evaluation