What does "cache-friendly" code mean and how to write efficient programs in C++?
Hello everyone! I'm a beginner C++ developer, writing small programs for data processing. Recently I came across the term "cache-friendly" and realized that my programs are probably running slower than they could.\n\nI have a simple example: I'm processing a 10000x10000 two-dimensional array. The first method is row-major traversal (outer loop over i, inner loop over j). The second is column-major traversal (outer loop over j, inner loop over i). On my laptop, the first option executes in 0.3 seconds, while the second takes 2.5 seconds! The difference is almost 8 times, even though the code does the same thing.\n\nHere's the code for illustration:\n\n```cpp\nconst int N = 10000;\nint arr[N][N];\n\n// Option 1: row-major (fast)\nfor (int i = 0; i < N; i++) {\n for (int j = 0; j < N; j++) {\n arr[i][j] = i * j;\n }\n}\n\n// In
No answers yet.
Log in to leave an answer