简体   繁体   中英

Stress Testing C++ program on local machine

I wrote a C++ program that take array as an input and calculates the max product of the pair of 2 elements from this array.

For example Input 3 elements:- 1,2,3 Max Product = 3*2 = 6. 

Another Example Input 5 elements :- 4,5,2,3,6 Max Product = 6*5 = 30

I want to stress test it ie to analyse the time taken by code to find the solution. I want to give like 1 million inputs or make the size of input variables very large on my local machine to stress test it.

Can anyone tell me how this is possible on my local machine or is there any website to check the performance of C++ program?

If you need a lot of values from stdin, then create a file with 1 million random generated values in the proper format, and then, use something like:

cat filename | ./application

...or, generate random numbers and write them to stdout (however, most likely, it would be a bit slower), while reading them from stdin in your program:

./datagen | ./application

So, the task you needed, will be done.

PS If you're using Windows, the commands I've shown could be executed in PowerShell, as Windows Command Shell doesn't support pipes written the way I wrote them.

First of all Stress testing is used in the case when we have two solutions. 1>Correct but slow which cannot be submitted in answer. (Time limit exceed case). 2>Wrong but fast which has to be submitted. Now code for a stress test is attached under. We take two functions. First function return output acc to 1st case and the second function returns the output of the 2nd case. Now we compare both the result by randomly generating numbers or arrays and compare in an infinite loop. See the code for further explanation.

while(true){                //infinite while loop
int n=rand()%1000+2;    //randomly generating array size n
cout<<n<<"\n";          
vector<int> a;
for(int i==;i<n;i++)
{
    a.push_back(rand()%100000);//randomly generating array of size n
}
for(int i=0;i<n;++i)
{
    cout<<a[i]<<' ';

}
cout<<"\n";
long long answer1=function1(a);//putting array in function 1
long long answer2=function2(a);//putting array in function 2
if(answer1!=answer2)           //comparing results     
{
    cout<<"Wrong Answer :"<<answer1<<' '<<answer2<<"\n";
    break;

}
else
{
    cout<<"OK\n";
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM