Blame c++/contourgl/measure.cpp

49e693
/*
49e693
    ......... 2015 Ivan Mahonin
49e693
49e693
    This program is free software: you can redistribute it and/or modify
49e693
    it under the terms of the GNU General Public License as published by
49e693
    the Free Software Foundation, either version 3 of the License, or
49e693
    (at your option) any later version.
49e693
49e693
    This program is distributed in the hope that it will be useful,
49e693
    but WITHOUT ANY WARRANTY; without even the implied warranty of
49e693
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49e693
    GNU General Public License for more details.
49e693
49e693
    You should have received a copy of the GNU General Public License
49e693
    along with this program.  If not, see <http: licenses="" www.gnu.org="">.</http:>
49e693
*/
49e693
49e693
#include <iostream></iostream>
49e693
#include <iomanip></iomanip>
49e693
b09c5d
#include <time.h></time.h>
b09c5d
49e693
#include "measure.h"
49e693
#include "utils.h"
49e693
#include "glcontext.h"
49e693
49e693
49e693
using namespace std;
49e693
49e693
49e693
std::vector<measure*> Measure::stack;</measure*>
49e693
49e693
a04770
void Measure::init() {
a04770
	hide = !stack.empty() && stack.back()->hide_subs;
a04770
	hide_subs |= hide;
a04770
	tga = filename.size() > 4 && filename.substr(filename.size()-4, 4) == ".tga";
a04770
	if (!hide)
a04770
		cout << string(stack.size()*2, ' ')
a04770
		     << "begin             "
a04770
			 << filename
a04770
			 << endl << flush;
49e693
	stack.push_back(this);
b09c5d
b09c5d
	timespec spec;
b09c5d
	clock_gettime(CLOCK_MONOTONIC , &spec);
b09c5d
	t = spec.tv_sec*1000000000 + spec.tv_nsec;
49e693
}
49e693
49e693
Measure::~Measure() {
49e693
	if (!surface && tga) glFinish();
49e693
82f284
	long long dt;
82f284
	if (has_subs) {
82f284
		dt = subs;
82f284
		if (!repeats.empty()) {
82f284
			// remove 25% of minimal values and 25% of maximum values
82f284
			for(int i = (int)repeats.size()/10; i; --i) {
82f284
				vector<long long="">::iterator j, jj;</long>
82f284
				for(jj = j = repeats.begin(); j != repeats.end(); ++j) if (*j < *jj) jj = j;
82f284
				repeats.erase(jj);
82f284
				for(jj = j = repeats.begin(); j != repeats.end(); ++j) if (*j > *jj) jj = j;
82f284
				repeats.erase(jj);
82f284
			}
82f284
			// get average
82f284
			long long sum = 0;
82f284
			for(vector<long long="">::iterator j = repeats.begin(); j != repeats.end(); ++j) sum += *j;</long>
82f284
			dt += sum/repeats.size();
82f284
		}
82f284
	} else {
82f284
		timespec spec;
82f284
		clock_gettime(CLOCK_MONOTONIC , &spec);
82f284
		dt = spec.tv_sec*1000000000 + spec.tv_nsec - t;
82f284
	}
82f284
	Real ms = 1000.0*1e-9*(Real)dt;
f83e6b
a04770
	if (!hide)
a04770
		cout << string((stack.size()-1)*2, ' ') << "end "
a04770
			 << setw(8) << fixed << setprecision(3)
a04770
			 << ms << " ms - "
a04770
			 << filename
a04770
			 << endl << flush;
49e693
49e693
	if (tga) {
49e693
		if (surface)
49e693
			Utils::save_surface(*surface, filename);
49e693
		else
49e693
			Utils::save_viewport(filename);
49e693
	}
49e693
49e693
	if (surface) {
49e693
		surface->clear();
f29469
	} else
f29469
	if (tga) {
49e693
		glClear(GL_COLOR_BUFFER_BIT);
49e693
		glFinish();
49e693
	}
49e693
49e693
	stack.pop_back();
82f284
	if (!stack.empty()) {
82f284
		stack.back()->has_subs = true;
82f284
		if (repeat) stack.back()->repeats.push_back(dt);
82f284
		       else stack.back()->subs += dt;
82f284
	}
49e693
}
49e693