notexOPENOverview
Notex/Languages/Pointer Passing Performance
04.08LESSON 08 OF 20 · 1 MIN READ

Pointer Passing Performance

Always free to read

If a struct is large enough, you can improve its performance by using a pointer to the struct as either an input parameter or a return value. The time to pass a pointer into a function is constant for all data sizes: roughly one nanosecond. This makes sense, since a pointer is the same size for all data types. In contrast, passing a value into a function takes longer as the data gets larger: once the value gets to be around 10 MB of data, for instance, passing it will take about 0.14 milliseconds.

The behavior for returning a pointer versus returning a value is more interesting. For data structures smaller than 100,000 bytes, it’s actually slower to return a pointer type than a value type. For example, it takes around 7 nanoseconds to return a 100-byte data structure, but about 24 nanoseconds to return a pointer to that data structure. As your data structures get larger, the performance advantage flips. It takes around.87 milliseconds to return 10 megabytes of data, but only.28 milliseconds to return a pointer to it.1

You should be aware that these are very short times. For the vast majority of cases, the difference between using a pointer and a value won’t affect your program’s performance. But if you are passing megabytes of data between functions, consider using a pointer, even if the data is meant to be immutable.

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K