Megosztás a következőn keresztül:


Útmutató: Az Alloc és a Free használata a memória teljesítményének javításához

Ez a dokumentum bemutatja, hogyan használhatók a concurrency::Alloc és concurrency::Free függvények a memória teljesítményének javítására. Összehasonlítja a tömb elemeinek párhuzamos megfordításához szükséges időt három különböző típus esetében, amelyek mindegyike megadja a new és delete operátorokat.

A Alloc és Free a függvények akkor a leg hasznosak, ha több szál gyakran hívja mindkettőt Alloc és Free. A futtatókörnyezet minden szálhoz külön memória-gyorsítótárat tartalmaz; ezért a futtatókörnyezet zárolások vagy memóriakorlátok használata nélkül kezeli a memóriát.

Példa: Új és törlési operátorokat meghatározó típusok

Az alábbi példa három típust mutat be, amelyek mindegyike megadja a new és a delete operátorokat. Az new_delete osztály a globális new és delete operátorokat használja, a malloc_free osztály a C futtatókörnyezet malloc és free függvényeit, és a osztály az egyidejűségi futtatókörnyezet és Alloc_Free függvényeit használja.

// A type that defines the new and delete operators. These operators 
// call the global new and delete operators, respectively.
class new_delete
{
public:
   static void* operator new(size_t size)
   {
      return ::operator new(size);
   }
   
   static void operator delete(void *p)
   {
      return ::operator delete(p);
   }

   int _data;
};

// A type that defines the new and delete operators. These operators 
// call the C Runtime malloc and free functions, respectively.
class malloc_free
{
public:
   static void* operator new(size_t size)
   {
      return malloc(size);
   }
   static void operator delete(void *p)
   {
      return free(p);
   }

   int _data;
};

// A type that defines the new and delete operators. These operators 
// call the Concurrency Runtime Alloc and Free functions, respectively.
class Alloc_Free
{
public:
   static void* operator new(size_t size)
   {
      return Alloc(size);
   }
   static void operator delete(void *p)
   {
      return Free(p);
   }

   int _data;
};

Példa: függvények felcserélése és reverse_array

Az alábbi példa a swap és reverse_array függvényeket mutatja be. A swap függvény a tömb tartalmát a megadott indexeken cseréli le. Memóriát foglal le a halomból az ideiglenes változó számára. A reverse_array függvény létrehoz egy nagy tömböt, és kiszámítja azt az időt, amely a tömb több alkalommal történő megfordításához szükséges.

// Exchanges the contents of a[index1] with a[index2].
template<class T>
void swap(T* a, int index1, int index2)
{
   // For illustration, allocate memory from the heap.
   // This is useful when sizeof(T) is large.
   T* temp = new T;
   
   *temp = a[index1];
   a[index1] = a[index2];
   a[index2] = *temp;
   
   delete temp;
}

// Computes the time that it takes to reverse the elements of a 
// large array of the specified type.
template <typename T>
__int64 reverse_array()
{
    const int size = 5000000;
    T* a = new T[size];   
    
    __int64 time = 0;
    const int repeat = 11;

    // Repeat the operation several times to amplify the time difference.
    for (int i = 0; i < repeat; ++i)
    {
        time += time_call([&] {
            parallel_for(0, size/2, [&](int index) 
            {
                swap(a, index, size-index-1); 
            });
        });
    }

    delete[] a;
    return time;
}

Példa: wmain függvény

Az alábbi példa a wmain függvényt mutatja be, amely kiszámítja azt az időt, amely ahhoz szükséges, hogy a reverse_array függvény a , new_deleteés malloc_free típusok szerint Alloc_Freejárjon el, amelyek mindegyike egy másik memóriafoglalási sémát használ.

int wmain()
{  
   // Compute the time that it takes to reverse large arrays of 
   // different types.

   // new_delete
   wcout << L"Took " << reverse_array<new_delete>() 
         << " ms with new/delete." << endl;

   // malloc_free
   wcout << L"Took " << reverse_array<malloc_free>() 
         << " ms with malloc/free." << endl;

   // Alloc_Free
   wcout << L"Took " << reverse_array<Alloc_Free>() 
         << " ms with Alloc/Free." << endl;
}

Példa a teljes kódra

A teljes példa a következő.

// allocators.cpp
// compile with: /EHsc 
#include <windows.h>
#include <ppl.h>
#include <iostream>

using namespace concurrency;
using namespace std;

// Calls the provided work function and returns the number of milliseconds 
// that it takes to call that function.
template <class Function>
__int64 time_call(Function&& f)
{
   __int64 begin = GetTickCount();
   f();
   return GetTickCount() - begin;
}

// A type that defines the new and delete operators. These operators 
// call the global new and delete operators, respectively.
class new_delete
{
public:
   static void* operator new(size_t size)
   {
      return ::operator new(size);
   }
   
   static void operator delete(void *p)
   {
      return ::operator delete(p);
   }

   int _data;
};

// A type that defines the new and delete operators. These operators 
// call the C Runtime malloc and free functions, respectively.
class malloc_free
{
public:
   static void* operator new(size_t size)
   {
      return malloc(size);
   }
   static void operator delete(void *p)
   {
      return free(p);
   }

   int _data;
};

// A type that defines the new and delete operators. These operators 
// call the Concurrency Runtime Alloc and Free functions, respectively.
class Alloc_Free
{
public:
   static void* operator new(size_t size)
   {
      return Alloc(size);
   }
   static void operator delete(void *p)
   {
      return Free(p);
   }

   int _data;
};

// Exchanges the contents of a[index1] with a[index2].
template<class T>
void swap(T* a, int index1, int index2)
{
   // For illustration, allocate memory from the heap.
   // This is useful when sizeof(T) is large.
   T* temp = new T;
   
   *temp = a[index1];
   a[index1] = a[index2];
   a[index2] = *temp;
   
   delete temp;
}

// Computes the time that it takes to reverse the elements of a 
// large array of the specified type.
template <typename T>
__int64 reverse_array()
{
    const int size = 5000000;
    T* a = new T[size];   
    
    __int64 time = 0;
    const int repeat = 11;

    // Repeat the operation several times to amplify the time difference.
    for (int i = 0; i < repeat; ++i)
    {
        time += time_call([&] {
            parallel_for(0, size/2, [&](int index) 
            {
                swap(a, index, size-index-1); 
            });
        });
    }

    delete[] a;
    return time;
}

int wmain()
{  
   // Compute the time that it takes to reverse large arrays of 
   // different types.

   // new_delete
   wcout << L"Took " << reverse_array<new_delete>() 
         << " ms with new/delete." << endl;

   // malloc_free
   wcout << L"Took " << reverse_array<malloc_free>() 
         << " ms with malloc/free." << endl;

   // Alloc_Free
   wcout << L"Took " << reverse_array<Alloc_Free>() 
         << " ms with Alloc/Free." << endl;
}

Ez a példa a következő mintakimenetet hozza létre egy négy processzorral rendelkező számítógéphez.

Took 2031 ms with new/delete.
Took 1672 ms with malloc/free.
Took 656 ms with Alloc/Free.

A példában a Alloc és Free függvényeket használó típus biztosítja a legjobb memóriateljesítményt, mivel a Alloc és Free függvények úgy vannak optimalizálva, hogy gyakran osszák ki és szabadítsák fel a memóriablokkokat több szálból.

A kód összeállítása

Másolja ki a példakódot, és illessze be egy Visual Studio-projektbe, vagy illessze be egy elnevezett allocators.cpp fájlba, majd futtassa a következő parancsot egy Visual Studio parancssori ablakban.

cl.exe /EHsc allocators.cpp

Lásd még

memóriakezelési függvények
Alloc függvény
Szabad függvény