On 02/01/2018 19:27,
slks...@gmail.com wrote:
> Goes to show how much of a difference the braces make in math operations.
Yes. Without braces, the expression has to be recompiled each time
through, and there's really no way to be fast about that.
With the braces in correctly, it takes 35.6s on this machine. (I guess
my CPU is stepping faster than yours.) Changing to taking the explim and
step as optional arguments (a more Tcl-ish way of doing things here)
cuts the time down to 34.2s. Here's what that best version looks like
(the changes to [integrate] are comparatively trivial). NB: Tcl follows
normal PEMDAS rules so we don't need those nested [expr] calls either.
proc myexp {val {explim 100}} {
set sum 0.0
set fact 1.0
set x 1.0
for {set i 1} {$i < $explim} {incr i} {
set fact [expr {$fact * $i}]
set x [expr {$x * $val}]
set sum [expr {$sum + $x / $fact}]
}
return [expr {$sum + 1.0}]
}
The major problem with the performance of all this is the cost of boxing
and unboxing all those Tcl_Obj* values, and that's a known *massive*
problem that we're working on; it's the subject of the BIG FlightAware
prize challenge. Running the code through our experimental tclquadcode
compiler leads me to estimate that it would take around 0.38s (excluding
the cost of compilation, which would actually be small by comparison
with the original runtime here). I can't compare with your figures
directly, but under a uniform scaling assumption (NB: THIS IS UNSAFE!)
I'd expect execution time around 0.57s, which should be pretty similar
to the Javascript and C. The acceleration is just a little under 90×
(you probably shouldn't read more than one significant figure in that!)
and is in line with what we'd expect from heavy numeric code with
multiple procedures. (There's a lot of uncertainty in this due to the
fact that our difference in processors could hide all sorts of weird
things. Alas, asking you to just build with tclquadcode isn't yet a
viable option; we're still pre-alpha for a bunch of unrelated reasons.)
Also, I think your figures for C are well out. Did you not build it in
optimized form? It should be a little faster than the Javascript.
Donal.
--
Donal Fellows — Tcl user, Tcl maintainer, TIP editor.