Ok, so I'm trying to prove to my teacher that Div and Mod isn't always the best way to solve things. So i created this tiny program, held entirely in main.cpp :
//Project: Mod_Efficiency
//Author : Kyle Wesley
//Descrip: This was made to prove that mod/div operations are often quite inefficient compared to their more basic
// counterparts...
#include<ctime>
#include<iostream>
using namespace std;
void DoEfficiencyDebug();
int main()
{
int ROW(100);
int iX(0); //coordinate x
int iY(0); //coordinate y
cout << "Doing 10000000 mod + divide calculations...";
for (int i = 1; i < 10000000; i++)
{
iX = i % ROW;
iY = i / ROW;
}
//clock();
/*cout << "Doing 10000000 non mod calculations...";
for (int i = 1; i < 10000000; i++)
{
iX++;
if (iX >= ROW)
{
iY++;
iX = 0;
}
}
*/
DoEfficiencyDebug();
cin.get();
return 0;
}
void DoEfficiencyDebug()
{
cout << "\n\nClock ticks for calculation: " << clock();
cout << "\n@ " << CLOCKS_PER_SEC << " For Duration: " << clock() / (CLOCKS_PER_SEC / 1000) << " milliseconds";
return;
}
The div/mod code and the basic iteration both do essentially the same thing (in the program we had to make) which is to plot squares appropriately on a grid. My only issue is that i'm not sure how to reset the clock(), so it has to be run in 2 different instances - one with mod/div commented out, one with it commented in. Does anyone know a reset clock() method?
For anyone who's interested, here's the results from this program (in terms of efficiency):
For 10000000 Calculations:
Mod/Div method : 1492 clock ticks or about 1500 milliseconds
Basic Iteration : 160 clock ticks or about 165 milliseconds
You're all welcome to show your instructors this =) . Although mod/div is occassionally neccessary.