View Full Version: Computer speed calculator

C++ Learning Community > C++ Creations > Computer speed calculator


Title: Computer speed calculator
Description: cpu speed, and averager


Excaliber7388 - March 9, 2006 08:29 PM (GMT)
I made this to check the speed of my computer. I took 5 scores and averaged them, the average (after closing all windows except my compiler) was 68498.2. How'd it do? Code is posted below, as well as the averaging program I made (Playing with data, I know pointers wern't necessary, it was just to learn them better). ^_^
CODE
//computer speed
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
   clock_t delay = 10* CLOCKS_PER_SEC;
   int i;
   clock_t start=clock();
   for (i=0;clock()-start<delay;i++)
   {
       cout <<"...Testing...\n";
   }
   cout <<"After 10 seconds, " << i << " loops were done\n";
   system("pause");
   return 0;
}

CODE
//averager
#include<iostream>
using namespace std;
int main()
{
   int * numbers=new int;
   cout<<"********************\n";
   cout << "   Averager   \n";
   cout<<"********************\n\n";
   cout<<"Please enter the number of numbers you wish to average\n";
   cin >> *numbers;
   cin.clear();
   double *sum=new double;
   double *a_number=new double;
   for(int i=0;i< *numbers;i++)
   {
           cout << "Please enter a number\n";
           cin >> *a_number;
           cout<< "\n";
           *sum += *a_number;
    }
   cout << "The sum is "<< *sum<<"\n";
   cout << "and the average is " << *sum / *numbers <<"\n";
   system("pause");
   delete numbers;
   delete a_number;
   delete sum;
   return 0;
}



C-Man - March 9, 2006 08:40 PM (GMT)
counting FLOP's eh ?

Excaliber7388 - March 10, 2006 01:17 AM (GMT)
Now I'm testing my computer all the time XD Once I learn more I might make it into a file manager, telling you when your computer is slowing down too much, so you can tell when it's going to freeze...although it should be obvious if you have 100 windows open, but sometimes it's just a bug, etc. Anyway, I'm pretty far off from that anyway XD

Rmstn1580 - March 10, 2006 01:22 AM (GMT)
If you make the loop say nothing you get a lot more loops :) Also, when you delete pointers, don't you need to set them to null (0)?

Excaliber7388 - March 10, 2006 01:59 AM (GMT)
I didn't read anything about that. And i know i could get more loops, but I'd rather not have 100000 loops ;) plus, in doing this, the user knows that the program is still running. (first, i had it output the value of i, but i figured this was more accurate.) It really does calculate it well, ^^ i use it all the time now. I'd like someone else to use it, and post the results, so I can have some proof that my computer's slow XD some incentive to spend a couple thousand dolars XD

Rmstn1580 - March 10, 2006 02:14 AM (GMT)
Well with iTunes open and playing, Mozilla Firefox open (one window), AOL Instant Messenger and MSN Instant Messenger both running, and Dev-C++ open, I got 127728393 loops. I'm improving your code to write out files and every time you run the program, it writes the loop count to a text file. Then the next time you run it, it tells you if you're speed has increased or decreased from the last run. Still have some things to do.... Thinking of putting some items in header files so code is easier to maintain.

xdracox - March 10, 2006 03:02 AM (GMT)
I rewrote your code for my own convenience.
CODE
//:main.cpp
// run a certain amount of tests and average the speeds
#include<iostream>
#include<ctime>

typedef unsigned int uint;
typedef unsigned long ulong;

int testNumber = 1;

/** run a test for a certain amount of seconds
* @param  unsigned int seconds - number of seconds to run the test for (in seconds)
* @return void
*/
uint runTest(unsigned int seconds);
void badInput();

int main()
{
 std::cout << "Computer Speed Tester" << std::endl;
 
 bool exit = false;
 
 do
 {
   std::cout << "Enter the number of times to test, 0 to exit: ";
   uint tests;
   std::cin >> tests;
   
   if ( !std::cin.good() )
   {
     badInput();
     continue;
   }
   
   if ( tests == 0 )
   {
     exit = true;
     continue;
   }
   
   std::cout << "Enter the length of each test (in seconds): ";
   uint seconds;
   std::cin >> seconds;
   
   if ( !std::cin.good() )
   {
     badInput();
     continue;
   }
   
   uint results = 0;
   
   for ( int testLoop = 0; testLoop < tests; testLoop++ )
     results += runTest(seconds);
   testNumber = 1;
   
   long average = results / tests;
   
   std::cout << "\nYour average amount of loops is: " << average << "\n\n" << std::endl;
 }
 while ( !exit );
 
 return 0;
}

uint runTest(uint seconds)
{
 std::cout << "\nRunning test # " << testNumber << ", please wait..." << std::endl;
 
 clock_t delay = seconds * CLOCKS_PER_SEC;
 clock_t start = clock();
 ulong testLoop;
 
 for ( testLoop = 0; clock() - start < delay; testLoop++ );
 
 std::cout << "Your computer ran " << testLoop << " loops in " << seconds
   << " seconds" << std::endl;
 
 testNumber++;
 
 return testLoop;
}

void badInput()
{
 std::cin.clear();
 std::cin.ignore(INT_MAX, '\n');
 std::cout << "ERROR: Bad input, please try again." << std::endl;  
}  


I ran 5 tests each 10 seconds. The average was: 150228830 loops

This was with XChat, GAIM, Firefox, and Dev-C++ open on Windows.

I'll test it out on Linux later, probably tomorrow.

Excaliber7388 - March 10, 2006 05:53 PM (GMT)
Great, now I feel like a scripting noob, AND your computer is a LOT faster :P This script is a bit above my level, but i think I can learn from it ^_^ Thanks

Viper - March 10, 2006 06:59 PM (GMT)
CODE
#include <windows.h>
#include <iostream>

union __lollercopter { __int64 i64; struct { unsigned i32a,i32b; }; }a;

__int64 GetCurrentCycles()
{
asm("rdtsc");
asm("movl %%eax,%0":"=r"(a.i32a));
asm("movl %%edx,%0":"=r"(a.i32b));
return a.i64;
}

int main()
{
LARGE_INTEGER sysfreq, time;
__int64 et, result;
QueryPerformanceFrequency(&sysfreq);
QueryPerformanceCounter(&time);
et = sysfreq.QuadPart + time.QuadPart;
__int64 start = GetCurrentCycles();
while(time.QuadPart < et) QueryPerformanceCounter(&time);
result = GetCurrentCycles() - start;
std::cout << "You can execute approximately " << result << " instructions per second";

return 0;
}


Compile with G++.
This should give you a pretty accurate number.. And having other programs open shouldn't affect it.
Yay @ C-Man for the __lollercopter code, yay @ me for the rest.

C-Man - March 10, 2006 07:14 PM (GMT)
you should set real time priority to prevent from being switched off ;)

Viper - March 10, 2006 07:15 PM (GMT)
Bah it works :P

Excaliber7388 - March 11, 2006 03:21 AM (GMT)
This is what i have now (without taking in what you guys did yet, I'll get to it ;) It now records to a txt file. What i would like to know, is if i can get it to write the date and time in there, and how I'd do it.
CODE
//computer speed
#include<iostream>
#include<fstream>
#include<ctime>
using namespace std;
int main()
{
   clock_t delay = 10* CLOCKS_PER_SEC;
   int i;
   string yn;
   clock_t start=clock();
   for (i=0;clock()-start<delay;i++)
   {
       cout <<"...Testing...\n";
   }
   cout <<"\aAfter 10 seconds, " << i << " loops were done\n";
   cout << "Record this? y/n _\b";
   cin >> yn;
   if(yn=="y")
   {
     ofstream myFile;
     myFile.open("cpu_speed_test_results.txt",ios::app);
     myFile << "Last test results: " << i << " loops \n";
     myFile.close();
     if (!myFile)
     {
        cout << "Error in file process." << endl;
        system("pause");
      }
     else
     {
       cout << "Write successful!" << endl;
       system("pause");
      }
  }
   
   return 0;
}

AquaFox - March 11, 2006 01:32 PM (GMT)
I tested Viper's code and I got
QUOTE
You can execute approximately 3192070008 instructions per second.

Excaliber7388 - March 11, 2006 02:58 PM (GMT)
Man, my computer IS really slow X_X
anyway, this is the latest addition, it allows you to write in your own notes, up to 100 characters
CODE
//computer speed
#include<iostream>
#include<fstream>
#include<ctime>

using namespace std;
int main()
{
   clock_t delay = 10* CLOCKS_PER_SEC;
   int i;
   string yn;
   char notes[100];
   clock_t start=clock();
   for (i=0;clock()-start<delay;i++)
   {
       cout <<"...Testing...\n";
   }
   cout <<"\aAfter 10 seconds, " << i << " loops were done\n";
   cout << "Record this? y/n _\b";
   cin >> yn;
   if(yn=="y")
   {
     ofstream myFile;
     myFile.open("cpu_speed_test_results.txt",ios::app);
     myFile << "Last test results: " << i << " loops \n";
     myFile.close();
     if (!myFile)
     {
        cout << "Error in file process." << endl;
        system("pause");
      }
     else
     {
       cout << "Write successful!" << endl;
     }
      cin.clear();
      cout << "\nAdd Notes? y/n \n";
      cin >> yn;
      if(yn=="y")
      {
        cout << "Enter Notes";
        cin.clear();
        cin.get();
        cin.getline(notes,100);
        ofstream myFile;
        myFile.open("cpu_speed_test_results.txt",ios::app);
        myFile << " : Notes : " << notes << "\n";
        myFile.close();
      }
       
  }
   
   return 0;
}

Excaliber7388 - March 15, 2006 08:43 PM (GMT)
Most recent changes:
New Menu, option to read, timestamp on recorded speeds :D
CODE

//V2-computer speed with more options
#include<iostream>
#include<fstream>
#include<ctime>
int menu();
using namespace std;
int choice;
int main()
{
   while (true)
   {
     choice=menu();  
     if (choice==1)
     {
       clock_t delay = 10* CLOCKS_PER_SEC;
       int i;
       string yn;
       char notes[100];
       clock_t start=clock();
       for (i=0;clock()-start<delay;i++)
       {
           cout <<"...Testing...\n";
       }
       cout <<"\aAfter 10 seconds, " << i << " loops were done\n";
       cout << "Record this? y/n _\b";
       cin >> yn;
       if(yn=="y")
       {
         time_t rawtime;
         struct tm * timeinfo;
         time ( &rawtime );
         timeinfo = localtime ( &rawtime );

         ofstream myFile;
         myFile.open("cpu_speed_test_results.txt",ios::app);
         myFile << "Last test results on " << asctime (timeinfo)  << " Loops: " << i << "\n";
         myFile.close();
         if (!myFile)
         {
            cout << "Error in file process." << endl;
            system("pause");
          }
         else
         {
           cout << "Write successful!" << endl;
         }
          cin.clear();
          cout << "\nAdd Notes? y/n \n";
          cin >> yn;
          if(yn=="y")
          {
            cout << "Enter Notes\n";
            cin.clear();
            cin.get();
            cin.getline(notes,100);
            ofstream myFile;
            myFile.open("cpu_speed_test_results.txt",ios::app);
            myFile << " : Notes : " << notes << "\n";
            myFile.close();
            if (!myFile)
            {
            cout << "Error in file process.\n" << endl;
            system("pause");
            }
            else
           {
           cout << "Write successful!\n" << endl;
           system ("pause");
           }
         }
       }  
   }//if 1 end
   if(choice==2)
   {
    ifstream   file("cpu_speed_test_results.txt");
    cout << file.rdbuf();
    system("pause");
   }
 }//while end
   return 0;
}
int menu()
{
   cout << "\n" << "---------------\n Enter Choice: \n 1)Run CPU speed test\n 2)Read Previous tests (NOT COMPLETE DO NOT RUN)\n 3)Exit\n---------------\n Choice:_\b";
   cin >> choice;
   if(choice==3)
   {
      exit(0);
    }
    else
      return choice;
}
 

Neken - March 16, 2006 02:20 AM (GMT)
QUOTE (Rmstn1580 @ Mar 9 2006, 08:22 PM)
Also, when you delete pointers, don't you need to set them to null (0)?

No.

Rmstn1580 - March 16, 2006 05:26 AM (GMT)
QUOTE (Neken @ Mar 16 2006, 02:20 AM)
QUOTE (Rmstn1580 @ Mar 9 2006, 08:22 PM)
Also, when you delete pointers, don't you need to set them to null (0)?

No.

Ya I just re-read that section in my book. I was thinking of something else.

Viper - March 16, 2006 01:28 PM (GMT)
Well it's nice doing it.. Because you don't want to delete a 0 pointer.

Excaliber7388 - January 4, 2007 10:19 PM (GMT)
I'm going to update this a bit. Current version is below. I plan to add a gui using Carbon.
However, counting loops isn't so great. I'd like to add the ability to calculate PI, the instructions per second that was posted earlier, and any other ways of testing.
But:
1) I don't know how to do that
2) I'd like more ideas for tests
CODE

/*
CPU Speed Test V 8
Compatability version-No Monitoring
Monitoring is not so usefull, I've found it uses up too many resources on older systems to be usefull for
computing.
*/
#include <iostream>
#include <fstream>
#include <ctime>

using namespace std;

int menu();

int main (int argc, char * const argv[])
{
 int choice;
 char yn;
 char notes[100];
 register long speed;
 int delay;
 choice = menu();
 while(choice>0)
 {
   if(choice==1)
{
  cout << "\nEnter number of whole seconds for test\n";
  cin >> delay;
  if(!cin)
  {
    exit(0);
  }
  cout << "\nTesting, please wait\n";
  clock_t test = delay* CLOCKS_PER_SEC;
     clock_t start=clock();
     while(clock()-start<test)
     {
       speed++;
     }
     speed/=delay*1000;
  cout << "Loops per millisecond: " << speed;
  time_t rawtime;
  struct tm * timeinfo;
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  ofstream myFile;
  myFile.open("radar_gun.txt",ios::app);
  myFile << "\nLast test results on " << asctime (timeinfo)  << " Test Length: " << delay << "\n  Loops/ms: " << speed << "\n";
  cout << "\nAdd notes? y/n \n";
  cin >> yn;
  if(!cin)
  {
    myFile.close();
 exit(0);
  }
  else
  {
    if(yn=='y')
 {
   cout << "\nEnter notes\n  ";
   cin.get();
   cin.clear();
    cin.get(notes,100);
   myFile << "\n    ~ Notes: " << notes << "\n";
 }
  }
  myFile.close();
  cin.get();
     cin.clear();
}
if(choice==2)
   {
    ifstream   file("radar_gun.txt");
    cout << file.rdbuf();
    cin.get();
 cin.get();
   }
choice = menu();
 }
 return 0;
}
int menu()
{
 int choice_menu;
 cout << "\n"
      << "~~~~~~~~~~~~~~~~~~~~\n"
   << "Welcome to Radar Gun V.2.0!\n"
   << " 0) Quit\n"
   << " 1) Test\n"
   << " 2) Read Previous results\n"
   << "~~~~~~~~~~~~~~~~~~~~\n";
 cin >> choice_menu;
 return choice_menu;
}

Excaliber7388 - January 15, 2007 11:09 PM (GMT)
Bump!




Hosted for free by InvisionFree