External Refs in Compile'd: Speed Impact
- To: mathgroup at smc.vnet.net
- Subject: [mg42669] External Refs in Compile'd: Speed Impact
- From: frankeye at cox.net (Frank Iannarilli)
- Date: Sat, 19 Jul 2003 03:20:04 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hi, (I'm running Mathematica 5.0 on Win2kPro). On this newsgroup, there has been continued discussion over the factors that impact the speed of Compile'd expressions. One helpful thread indicated that the presence of external references in the Compile'd opcode stream would substantially slow execution. {One can check this by, e.g., foo=Compile[...], then evaluating foo[[4]], which contains the opcode stream list}. The Book indicates that Compile[] can handle references to many Mathematica functions and constructs, and indeed it can (e.g.: foo = With[{a = Function[p, Sinh[p*2.]]}, Compile[{x}, a[x]]] foo[[4]] is pure opcode stream, no external reference to Sinh). Erf is a function that, if included in a Compile[] expression, evidently does NOT get digested by Compile, and instead results in an external reference. Given the nature of Erf (with possibly complex args), this is understandable. However, what's the impact of such an external reference on speed? For real arguments, I crafted my own Compile'd Erf[], named fErf[], and referenced fErf instead of Erf within my Compile'd expression. Compile[] essentially "transcludes" fErf[]'s own Compile'd code within my Compile'd expression. The speed difference is a FACTOR of 5!! (i.e. inclusion of fErf[] is 5x faster than external ref to Erf[]). This is so even though fErf[] is *itself* a factor of 5 slower than Erf[] (in evaluating them each 10,000 times by Table[]). Therefore, external references to "Compile-irreducible" functions can have substantial adverse impact on speed. Hope this provides a concrete example of a speed impact factor. Cheers,