Here are two programs with big (1_048_576 bytes) array.
They are almost identical, the only difference is:
fpaqi16-stack
holds that big array on the stackfpaqi16-help
holds that big array on the heap
Array on the stack:
- compiling in Debug mode
$ zig build-exe fpaqi16-stack.zig -fsingle-threaded
$ ./fpaqi16-stack c ~/CC/obj1 z.z
obj1 (21504 bytes) -> z.z (13852 bytes) in 29684 msec !!!!!!!
As can be seen it is pathologically slow.
- compiling in ReleaseFast mode
$ zig build-exe fpaqi16-stack.zig -fsingle-threaded -O ReleaseFast
$ ./fpaqi16-stack c ~/CC/obj1 z.z
obj1 (21504 bytes) -> z.z (13852 bytes) in 60 msec
60 msec (Fast) vs 30 sec (Debug)!!!
Array on the heap:
- compiling in Debug mode
$ zig build-exe fpaqi16-heap.zig -fsingle-threaded
$ ./fpaqi16-heap c ~/CC/obj1 z.z
obj1 (21504 bytes) -> z.z (13852 bytes) in 79 msec
- compiling in ReleaseFast mode
$ zig build-exe fpaqi16-heap.zig -fsingle-threaded -O ReleaseFast
$ ./fpaqi16-heap c ~/CC/obj1 z.z
obj1 (21504 bytes) -> z.z (13852 bytes) in 61 msec
No significant difference.
So the question is why does the combination of ‘array on the stack’ and “compiling in Debug mode” produce such a slow program?