If the benchmark makes only 512 iterations to process a list of only 4 elements, it is because the processing is (very) slow.
This is due to the 'print' function which monopolizes almost all the processing time.
This kind of benchmark is not reliable to compare 'repeat', 'while' and 'foreach'.
With a longer list, the use of 'repeat' would certainly be slower, not because of 'repeat' itself, but because of the use of 'nth'.
Comparing the performance of these three functions doesn't really make sense, because they don't have the same use, 'foreach' can only be used with a list, and doing list processing with 'repeat' is nonsense.
Let's try some more reliable comparison by building three functions to simply reverse a list.
(defun reverse_foreach
(l
/ r
) )
)
(defun reverse_while
(l
/ r
) )
)
r
)
(defun reverse_repeat
(l
/ i r
) )
)
r
)
Now let's try a new benchmark with a more significant list (256 items)
Benchmarking ................Elapsed milliseconds / relative speed for 8192 iteration(s):
(REVERSE_WHILE L).......1500 / 2.35 <fastest>
(REVERSE_FOREACH L).....1562 / 2.26
(REVERSE_REPEAT L)......3531 / 1.00 <slowest>
We can immediately see the relative inefficiency of repeat for this kind of task.
And if while seems to be faster than foreach, we should not focus on the "relative speed" (2.26 vs 2.35) but try to see what this represents concretely. The difference between foreach and while is 62 milliseconds for 8192 iterations, in other words about 0.0076 milliseconds for processing one list.