The Crystal Programming Language Forum

Fastest Looping construct in Crystal?

What is the fastest looping construct in Crystal?





loop do?

What are you looping over and why does it matter how fast the loop is? Unless you’re looping just to loop, whatever you do inside the loop is likely to be so much slower than the actual loop logic that it’s not worth considering what loop is fastest.


The specific application of a loop I am going to do is loop over a directory of filenames to read in the files. or maybe

Usually it’s best to use higher level iterators like this, as they can sometimes contain optimizations suited to a given purpose.


You won’t see a huge performance difference between any of them, because they usually result in pretty much the same logic anyway.


It absolutely doesn’t matter, reading from the file system is orders of magnitude slower than iterating over data in memory.

For the most part there’s no meaningful difference and it’s probably nearly identical IR code that gets passed to LLVM. If you need to be sure you could always write your code in every method and see which one runs the fastest and share the data with us.

Aside from the IR code newer LLVM versions have different optimizations in place so platform and LLVM version differences may result in slightly different results.

@Blacksmoke16 you understand the benchmarking stuff better than me, can you provide them with instructions on doing this?