I have created a program that prints out all of the permutations of the characters provided through command-line arguments and decided I wanted to compare execution time to an equivalent program written in Java.
The program worked until I decided to find the permutations multiple times in order to get an average execution time.
void avgTime(char**& argv, int times) {
if (sizeof(argv) > 1) {
long permutationAmnt;
clock_t s_start, s_end, t_start, t_end;
float s_runms, t_runms;
long avg_time;
for (int count = 0; count < times; count++) {
t_start = clock();
for (int i = 1; i < sizeof(argv); i++) {
s_start = clock();
permutationAmnt = permutations(std::string(argv[i]));
s_end = clock();
s_runms = ((float)s_end - s_start) / CLOCKS_PER_SEC * 1000;
std::cout << "SUCCESS (" << s_runms << "ms for " << permutationAmnt << " permutations)" << std::endl << std::endl;
}
t_end = clock();
t_runms = ((float) t_end - t_start) / CLOCKS_PER_SEC * 1000;
std::cout << std::endl << "TOTAL RUNTIME: " << t_runms << "ms" << std::endl;
avg_time += t_runms;
}
std::cout << "AVERAGE RUNTIME: " << avg_time / times << "ms" << std::endl;
}
}
int main(int argc, char** argv) {
avgTime(argv, 10);
return 0;
}
The first for-loop in avgTime()
only executes a single time (putting a cout inside of it only prints one time) and the program appears to terminate after the nested for-loop breaks.
I am not sure if the problem is with some of the code from avgTime()
or if it comes from one of the helper functions, like permute()
. Either way here is the code for each of the helper functions as well as the includes (p.s. num
is declared outside of any functions).
/*
* Calls the recursive permute() function then
* returns the total amount of permutations possible
* for the given input.
*
* NOTE: the num variable is used in the permute() function
* for numbering the permutations printed as output (see next function
* for clarificiation)
*/
long permutations(const std::string& arg) {
long totalPermutations = factorial(arg.size()); //self-explanatory
num = 1;
permute(arg, 0);
return totalPermutations;
}
/*
* Recursively prints out each permutation
* of the characters in the argument, str
*/
void permute(const std::string& str, int place) {
if (place == str.size() - 1) std::cout << ((num <= 10) ? "0" : "") << num++ << ". " << str << std::endl;
for (int i = place; i < str.size(); i++) {
permute(swap(place, i, str), place + 1); //self-explanatory
}
}
long factorial(int num) {
if (num < 2) {
return 1;
}
return factorial(num - 1) * num;
}
std::string swap(int i, int j, const std::string& str) {
std::string s(str);
s[i] = s[j];
s[j] = str[i];
return s;
}
NOTE: the permute()
function appears before the permutation()
function in the source code and is visible to all the necessary callers of it.
//Includes and namespace stuff
#include <iostream>
#include <string>
#include <time.h>
I would appreciate any help that you guys can offer, if there is any additional information that you would like me to provide just let me know. Thanks again for any help.
P.S. No, this isn't a homework assignment :P
EDIT: Removed using namespace std;
and adjusted the code accordingly to avoid confusion between the function std::swap()
and my own swap()
function. Also, added the swap()
and factorial()
functions to avoid any ambiguity. I apologize for the confusion this caused.
I was using sizeof(argv)
rather than just using argc
. Switching to the latter option fixed the issue.