All pastes #980035 Raw Edit

Untitled

public cpp v1 · immutable
·published 2008-04-10 19:23 UTC
rendered paste body
// -*- mode: c++ -*-// $Id: objinst.g++,v 1.10 2001/10/09 23:52:37 doug Exp $// http://www.bagley.org/~doug/shootout///** This copy courtesy of http://www.kano.net/javabench///**  Optimized object creation/destruction for C++//**  Added Timer() code#include <ctime>#include <iostream>#include <string>#include <cstdlib>using namespace std;class Toggle {public:    Toggle(bool start_state) : state(start_state) { }    virtual ~Toggle() {  }    bool value() {		return(state);    }    virtual Toggle& activate() {		state = !state;		return(*this);    }		protected:    bool state;};class NthToggle : public Toggle {public:    NthToggle(bool start_state, int max_counter) :			Toggle(start_state), count_max(max_counter), counter(0) { }    Toggle& activate() {		if (++this->counter >= this->count_max) {			state = !state;			counter = 0;		}		return(*this);    }private:    int count_max;    int counter;};int main(int argc, char *argv[]) {	int n = ((argc == 2) ? atoi(argv[1]) : 1);    clock_t start=clock();	Toggle toggle1(true);    for (int i=0; i<5; i++) {		cout << ((toggle1.activate().value()) ? "true" : "false") << endl;	}	for (int i=0; i<n; i++)		Toggle t(true);		cout << endl;     NthToggle ntoggle1(true, 3);	for (int i=0; i<8; i++) {	    cout << ((ntoggle1.activate().value()) ? "true" : "false") << endl;    }	for (int i=0; i<n; i++)		NthToggle ntoggle(true, 3);	clock_t endt=clock(); 	std::cout <<"Time: " << double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n"; 	return 0;}