There is a way to optimize this program?

Test my runnable code use latest Crystal night build.

 ╰──➤ $ cr version
Crystal 1.15.0-dev [d57c9d84d] (2024-11-20)

LLVM: 18.1.8
Default target: x86_64-pc-linux-gnu

It’s seem like original ysbaddaden version no big different, but, my changed version (small batches which equals to core number, with a large batch_size), slow a lot than before. time took: from 13s → 21s


Following is the result when I change the run order of two version.

 ╰──➤ $ 1  CRYSTAL_WORKERS=16 ./1
(@zw963 changed version)small batches, with a large batch_size, result is unpredictable, and slower(most of time).
total_seeds # => 1000000000
batch_size # => 62500000
worker_size # => 16
batches # => 16
Seed: 690000000 Steps: 195
Seed: 815000000 Steps: 110
Seed: 941000000 Steps: 211
Seed: 316000000 Steps: 199
Seed: 879000000 Steps: 136
Seed: 4000000 Steps: 15469
Seed: 191000000 Steps: 170
Seed: 812000000 Steps: 795
collatz took: 00:00:21.662288613
----------------------------------------------------------------------------------------------------
(@ysbaddaden original version)large batches, with a small batch_size, result correct, and faster.
total_seeds # => 1000000000
batch_size # => 1000
worker_size # => 16
batches # => 1000000
Seed: 999000000 Steps: 162
collatz took: 00:00:13.697945316
 ╰──➤ $ CRYSTAL_WORKERS=16 ./1
(@ysbaddaden original version)large batches, with a small batch_size, result correct, and faster.
total_seeds # => 1000000000
batch_size # => 1000
worker_size # => 16
batches # => 1000000
Seed: 999000000 Steps: 162
collatz took: 00:00:12.638324348
----------------------------------------------------------------------------------------------------
(@zw963 changed version)small batches, with a large batch_size, result is unpredictable, and slower(most of time).
total_seeds # => 1000000000
batch_size # => 62500000
worker_size # => 16
batches # => 16
Seed: 937000000 Steps: 224
collatz took: 00:00:21.383333880