Jon Harrop wrote:
> Sure. Generating exceptions unless absolutely necessary will be a very bad
> idea on the CLR but it will also be a bad idea on the JVM because its
> exception handling is slow.
not always; it's entirely possible that I recall wrongly, but I remember
that we had no penalty in using exception vs. a plain for loop.
> Sounds like you were translating that into something like (F# code):
>
> exception StopIteration
>
> let run() =
> try
> for i=0 to 2 do
> if foo i=0 then raise StopIteration
> with StopIteration ->
> ()
> bar()
> baz()
yes, more ore less
> But you could have translated it into:
>
> let rec run_1 i =
> if foo i=0 then run_2() else
> if i<3 then run_1 (i + 1) else run_2()
> and run_2() =
> bar()
> baz()
>
> let run() =
> run_1 0
>
> Where both calls to the continuation "run_2" inside the body of the "run_1"
> function are tail calls.
well, but doing such a translation is not straightforward; it's doable,
but you need to write it, exactly as we wrote our own exception inliner
that compiles the original code into a plain for loop.
Honestly, I doubt that a tail call can be faster/much faster than a
plain for loop, but we should do some benchmark of course.
> Doing a quick benchmark on this code, I find that 10^6 iterations using your
> exception-based technique gives:
>
> CLR: 24s
> JVM: 1.3s
>
> Holy smokes, the JVM is 18x faster!
>
> Now try the tail calls (only available on the CLR):
>
> CLR: 0.025s
>
> Holy smokes, the CLR is 52x faster!
how does this compare with the first version of the loop you wrote?
void run() {
for (int i=0; i<3; ++i)
if (foo(i) == 0) break;
bar();
baz();
}
ciao,
Anto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "JVM
Languages" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jvm-languages?hl=en
-~----------~----~----~----~------~----~------~--~---