Group Group Group Group Group Group Group Group Group

"Maps directly to actual computer hardware" - how would I know?

example("imperative, early-exit") {
  var total = 0
  for value in numbers {
    guard value >= 0 else { break }
    total += value
  }
  print(total)
}

“It also maps directly to actual computer hardware so the optimizer doesn’t have to work nearly as hard.”

Along what lines should I be thinking to come to this conclusion? What are the keywords I should be looking up to be able to draw these sort of conclusions?

1 Like

Can you take a look @rayfix ?

Thanks for the question. What I mean to express here is that because the solution is imperative and CPUs are also by their very nature, imperative state machines, the mapping (or lowering) to machine instructions is very direct.

In this very particular case, LLVM is so smart that I wouldn’t be surprised if it unrolled the loop and found the answer at compile time so that at runtime, it would just print a literal number.

Compiler lowering and optimization is a very deep subject and one could earn many PHDs exploring the space!

I don’t know if that answers your question but let me know if you would like more clarification.

2 Likes

One more thought… choosing the clearest programming style is many times the best way to go. Only worry about the performance after you have solved the problem correctly. :slight_smile:

2 Likes

Thanks Ray and Shai!

1 Like