0

質問のタイトルは非常に自明です。動的サイズの配列を必要とする実行ループがあります。ただし、そのサイズの最大値はわかっているので、必要に応じて、動的にサイズを変更する代わりに最大化できます。

これが私のコードです。移植性の観点からは、おそらく clock_t がタイミングの最良の選択ではないことはわかっていますが、clock_t は精度が悪いです。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <ctime>

#define TEST_SIZE 1000000

using namespace std;

int main(int argc, char *argv[])
{
    int* arrayPtr = NULL;
    int  array[TEST_SIZE];
    int  it = 0;

    clock_t begin, end;

    begin = clock();
    memset(array, 0, sizeof(int) * TEST_SIZE);
    end = clock();
    cout << "Time to memset: "<< end - begin << endl;

    begin = clock();
    fill(array, array + TEST_SIZE, 0);
    end = clock();
    cout << "Time to fill: "<< end - begin << endl;

    begin = clock();
    for ( it = 0 ; it < TEST_SIZE ; ++ it ) array[it] = 0;
    end = clock();
    cout << "Time to for: "<< end - begin << endl;
}

これが私の結果です:

Time to memset: 1590
Time to fill: 2334
Time to for: 2371

new & delete が配列をゼロにすることがわかったので、これらよりも速い方法はありますか?

私を助けてください!

4

4 に答える 4