// Timing code for question 4 on yellow test 3.
// CS 509, Fall 2002
#include <iostream>
#include "stopwatch.h"
typedef void (* loopf)(const std::string &, unsigned);
static void
do_loop(
const std::string & str1, unsigned iter_cnt, loopf f, const char * const label) {
std::string str2;
stopwatch sw;
sw.start();
f(str1, iter_cnt);
sw.stop();
std::cout << label << " " << str1.size() << " " << sw.elapsed()/iter_cnt << "\n";
}
static void
loop_a(const std::string & str1, unsigned iter_cnt) {
std::string str2;
for (unsigned i = 0; i < iter_cnt; i++) {
for (std::string::const_iterator si = str1.begin(); si != str1.end(); si++)
str2 += *si;
str2.clear();
}
}
static void
loop_b(const std::string & str1, unsigned iter_cnt) {
std::string str2;
for (unsigned i = 0; i < iter_cnt; i++) {
for (std::string::const_iterator si = str1.begin(); si != str1.end(); si++)
str2 = *si + str2;
str2.clear();
}
}
int
main() {
const unsigned iter_cnt = 200;
for (unsigned i = 1; i <= 10; i++) {
std::string str(i*1000, 'a');
do_loop(str, iter_cnt, loop_a, "a");
do_loop(str, iter_cnt, loop_1b, "b");
}
}
// $Log: t3q4.cc,v $
// Revision 1.1 2002/10/23 02:33:57 rclayton
// Initial revision
//
syntax highlighted by Code2HTML, v. 0.9