modified: src1/input.c
[GalaxyCodeBases.git] / BGI / BASE / src / 2bwt / Timing.c
blob1675f5833f69980e28d273f056d56a650055fd60
1 /*
3 Timing.c Measuring Program running time
5 This module contains functions for measuring program running time.
7 Copyright (C) 2004, Wong Chi Kwong.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #ifdef RUSAGE
30 #include <sys/resource.h>
31 #else
32 #ifdef TIME_BY_CLOCK
33 #include <time.h>
34 #else
35 #include <sys/time.h>
36 #endif
37 #endif
39 #include "Timing.h"
42 double setStartTime() {
44 #ifdef RUSAGE
46 double usertime, systime;
47 struct rusage usage;
49 getrusage(RUSAGE_SELF, &usage);
51 usertime = (double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000.0;
52 systime = (double)usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec / 1000000.0;
54 return(usertime + systime);
56 #else
57 #ifdef TIME_BY_CLOCK
59 return (double)clock() / (double)CLOCKS_PER_SEC;
61 #else
63 struct timeval tp;
64 gettimeofday(&tp, NULL);
65 return (double)tp.tv_sec + (double)tp.tv_usec / (double)1000000;
67 #endif
68 #endif
72 double getElapsedTime(double startTime) {
74 #ifdef RUSAGE
76 double usertime, systime;
77 struct rusage usage;
79 getrusage(RUSAGE_SELF, &usage);
81 usertime = (double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000.0;
82 systime = (double)usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec / 1000000.0;
84 return (usertime + systime) - startTime;
86 #else
87 #ifdef TIME_BY_CLOCK
89 return (double)clock() / (double)CLOCKS_PER_SEC - startTime;
91 #else
93 struct timeval tp;
94 gettimeofday(&tp, NULL);
95 return (double)tp.tv_sec + (double)tp.tv_usec / (double)1000000 - startTime;
97 #endif
98 #endif
102 void printElapsedTime(FILE *file, const int printHour, const int printMin, const int printSec,
103 const int secNumberOfDecimal, const double seconds) {
105 printElapsedTimeNoNewLine(file, printHour, printMin, printSec, 0, secNumberOfDecimal, seconds);
106 fprintf(file, "\n");
110 void printElapsedTimeNoNewLine(FILE *file, const int printHour, const int printMin, const int printSec,
111 const int secMinPrintLength, const int secNumberOfDecimal, const double seconds) {
113 int hour, min;
114 double sec;
115 char secondDisplay[8] = "%0.0f s";
117 #ifdef DEBUG
118 if (printHour && !printMin && printSec) {
119 fprintf(stderr, "printElapsedTime(): Cannot skip minute only!\n");
120 exit(1);
122 if (secNumberOfDecimal > 9) {
123 fprintf(stderr, "printElapsedTime(): secNumberOfDecimal > 9!\n");
124 exit(1);
126 #endif
128 secondDisplay[1] = secondDisplay[1] + (char)secMinPrintLength;
129 secondDisplay[3] = secondDisplay[3] + (char)secNumberOfDecimal;
131 sec = seconds;
132 min = (int)(seconds / 60);
133 if (!printSec && printMin) {
134 if (seconds - min * 60 >= 30) {
135 min++;
138 if (printMin) {
139 sec -= min * 60;
142 hour = min / 60;
143 if (!printMin) {
144 min = hour * 60;
145 if (min >= 30) {
146 hour++;
149 if (printHour) {
150 min -= hour * 60;
153 if (printHour) {
154 fprintf(file, "%d h ", hour);
156 if (printMin) {
157 fprintf(file, "%d m ", min);
159 if (printSec) {
160 fprintf(file, secondDisplay, sec);