Reusing memory in C ++

Just wondering if this code is recommended to improve performance?

void functionCalledLotsofTimes() { static int *localarray = NULL; //size is a large constant > 10 000 if (localarray == NULL) localarray = new int[size]; //Algorithm goes here } 

I am also interested in how static variables are implemented by modern C ++ compilers such as g ++. Are they treated as global variables?

+4
source share
4 answers

Not recommended because you are entering a global state in a function. When you have a global state in function, you have side effects. Side effects cause problems, especially in multithreaded programs.

See link transparency for more details. With the same input, you always want to have the same output, no matter how many threads you use.

If you want to activate greater efficiency, allow the user to specify the buffer as one of the parameters.

See the difference between global and static variables here .

+12
source

A significant increase in performance, but you have to be very careful how the data will be used. If multithreading, you must provide data access control.
run this test:

  #include <windows.h>
 #include <stdio.h>

 void StartTime (LARGE_INTEGER * pTime)
 {
     QueryPerformanceCounter (pTime);
 }

 ULONG EndTime (LARGE_INTEGER * pTime)
 {
     LARGE_INTEGER liDiff;
     LARGE_INTEGER liFreq;

     QueryPerformanceCounter (& liDiff);

     liDiff.QuadPart - = pTime-> QuadPart;
     liDiff.QuadPart * = 1000;  // Adjust to milliseconds, shouldn't overflow ...

     (void) QueryPerformanceFrequency (& liFreq);

     return ((ULONG) (liDiff.QuadPart / liFreq.QuadPart));
 }

 void functionCalledLotsofTimesStatic (int size)
 {
    static int * localarray = NULL;

    // size is a large constant> 10,000
    if (localarray == NULL) localarray = new int [size];  

    // Algorithm goes here

 }

 void functionCalledLotsofTimesDynamic (int size)
 {
    int * localarray = NULL;

    // size is a large constant> 10,000
    if (localarray == NULL) localarray = new int [size];  

    // Algorithm goes here

    delete [] localarray;
 }


 int _tmain (int argc, _TCHAR * argv [])
 {
     LARGE_INTEGER liTimeDynamic, liTimeStatic;

     StartTime (& liTimeDynamic);
     for (int loop = 0; loop <100000; loop ++)
     {
         functionCalledLotsofTimesDynamic (10000);
     }
     printf ("Time dynamic alocation:% lu milliseconds \ n", EndTime (& liTimeDynamic));

     StartTime (& liTimeStatic);
     for (int loop = 0; loop <100000; loop ++)
     {
         functionCalledLotsofTimesStatic (10000);
     }
     printf ("Time staic alocation:% lu milliseconds \ n", EndTime (& liTimeStatic));

     return 0;
 }

0
source

If I am not mistaken, the test that the static variable has already been initialized is still performed by the "system". No need to do it yourself. Just write

 static int *localarray = new int[size]; 

Only the first time a function is called, an array will be created.

0
source

Typically, such code is hidden behind a custom allocator class that has a large, pre-allocated buffer, so "dynamic allocations" are not really.

Many std :: vector implementations have a more basic form of implementing this: by default, it allocates memory "blocks" in powers of two, so no real new allocations need to be done until the vector is twice as large.

0
source

Source: https://habr.com/ru/post/1302319/


All Articles