0

The question title is quite self-explanatory. I have an run loop that need a dynamic-sized array. But I do know the maximum of that size is going to be, so if needed, I can max it out instead of dynamically-sizing it.

Here's my code, I know that clock_t probably not the best choice for timing in terms of portability, but clock_t provide bad accuracy.

#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;
}

Here's my result:

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

Now that I know new & delete does now zero-out the array, is there any way faster than these?

Please help me!

Shane Hsu
  • 7,937
  • 6
  • 39
  • 63

4 Answers4

3

Basically you are comparing apples and oranges.

memset and the for-loop explicitly set the memory content to a particular value(in your example 0). While, the new merely allocates sufficient memory(atleast as requested) and delete merely marks the memory free for reuse. There is no change in the content at that memory. So new and delete do not initialize/de-initialize the actual memory content.
Technically, the content in that memory has an Indeterminate value. Quite literally, the values maybe anything and you cannot rely on them to be anything specific.They might be 0 but they are not guaranteed to be. In fact using these values will cause your program to have an Undefined Behavior.

A new call for an class does two things:

  • Allocates requested memory &
  • Calls constructor for the class to initialize the object.

But note that in your case the type is an int and there is no default initialization for int.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

new only allocates a memory block, it doesn't initialize the allocated memory. To initialize an array you can use memset() or do it manually.

Johnny Mnemonic
  • 3,822
  • 5
  • 21
  • 33
0

Good compiler will optimize all 4 approaches into 1 call of memset. Also, what's the difference between 3rd and 4th approach?

You can also do

int  array[TEST_SIZE] = {};

gain readability and save 1 line of code.

Paul
  • 13,042
  • 3
  • 41
  • 59
0

I would resort to memset in this case. fill is generic, but the platform can offer you some really nice tricks in its implementation of memset. This is possible, because the function is unambigous in what it does and dumb enough:

  • It could employ (S)DMA for actual memory modification, which could have faster interface to memories. Also, while it runs the task, the CPU could do something else
  • When it knows it has to sequentially write contiguous memory region, it could do something preventive about cache invalidation
  • The implementation in ARM-based embedded systems can benefit from burst mode; it is realized with special assembler instruction (STMFD, STMFA etc.) and in this mode 3 writes are equal two normal writes timewise
Roman Saveljev
  • 2,544
  • 1
  • 21
  • 20