C++ code much faster than Julia how can I optimize it?

Just commenting on your comment to the comment about the code :wink:

With triply nested indexing, you need a pretty steady hand to ensure nothing goes out of bounds. Seems risky with regular Arrays, let alone general indexable collections.