vendredi 23 février 2018

Testing how fast new is on my computer, using it to allocate an array of doubles, ten thousand times?

I am having trouble doing this, I am new to C++, this is from the Joshi design and patterns book. So I would assume that I would need to make an array ten thousand times and test how fast it took, and then use new to make a dynamic set of arrays, ten thousand and then time it again. But I am confused on how to make ten thousand arrays, or run the program ten thousand times? So far this is the code I have written:

#include <stdio.h>
#include <ctime>
#include <iostream>
using namespace std;

int main()
{

    long start = clock();
    int size;
    cout << "\nEnter size of array\n";
    cin >> size;


//    double array[size];
//    for(int i=0; i<size ; i++) {
//        array[i] = rand() % 10000000;
//    }

    double *dynamicArray = new double[size];
    for(int i=0; i<size ; i++) {
        dynamicArray[i] = rand() % 10000000;
    }


    long end = clock();

    cout << "it took " << ((float)end-start)/CLOCKS_PER_SEC << " seconds." << endl;


}

Aucun commentaire:

Enregistrer un commentaire