Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use foreach instead of while with l_nth #235

Open
filipeom opened this issue Jan 31, 2025 · 0 comments · May be fixed by #252
Open

Use foreach instead of while with l_nth #235

filipeom opened this issue Jan 31, 2025 · 0 comments · May be fixed by #252
Labels
optimization Optimization of the implementation

Comments

@filipeom
Copy link
Member

filipeom commented Jan 31, 2025

The while with l_nth pattern is often used in the interpreter.

However, in my experimental evaluation:

function main() {
  // List with 10000 integer elements
  lst := [0, 1, ..., 9999]
  n := l_len(lst);
  i := 0;
  while (i < n) {
    v := l_nth(lst, i);
    print v;
    i := i + 1;
  }
  return 0;
}
function main() {
  // List with 10000 integer elements
  lst := [0, 1, ..., 9999]
  res := 0;
  foreach (v : lst) {
    res := res + v;
  }
  return 0;
}

is:

hyperfine "ecma-sl interpret while.esl" "ecma-sl interpret foreach.esl"
Benchmark 1: ecma-sl interpret while.esl
  Time (mean ± σ):     126.3 ms ±   3.4 ms    [User: 113.0 ms, System: 13.3 ms]
  Range (min … max):   121.3 ms … 134.6 ms    23 runs

Benchmark 2: ecma-sl interpret foreach.esl
  Time (mean ± σ):      47.9 ms ±   2.2 ms    [User: 34.9 ms, System: 12.9 ms]
  Range (min … max):    45.6 ms …  57.3 ms    64 runs

Summary
  ecma-sl interpret foreach.esl ran
    2.64 ± 0.14 times faster than ecma-sl interpret while.esl
  • 2.64 faster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization Optimization of the implementation
Projects
None yet
1 participant