[C++]用变量增长模拟算法运行时间

来源:互联网 发布:剑灵女帝捏脸数据 编辑:程序博客网 时间:2024/06/10 04:41

模拟算法运行所需时间

Description:

Carson write a simple program to solve Fabonaci and Factorial.
But he wants to know how it cost time when different inputs are given.
Now you are invited as an best expert to fix it.
Read main.cpp for details about the functions of

“`
standard input
1 2 3 4 5
6 7 8 9 10

standard output
input: 1, result: 1
input: 2, result: 2
input: 3, result: 2
input: 4, result: 4
input: 5, result: 5
line 0 - 1: 0.050ms
line 1 - 2: 0.050ms
line 2 - 3: 0.150ms
line 3 - 4: 0.250ms
line 4 - 5: 0.450ms
input: 6, result: 720
input: 7, result: 5040
input: 8, result: 40320
input: 9, result: 362880
input: 10, result: 3628800
line 0 - 1: 0.300ms
line 1 - 2: 0.350ms
line 2 - 3: 0.400ms
line 3 - 4: 0.450ms
line 4 - 5: 0.500ms
#Timer_1: 0.950ms
#Timer_2: 2.000ms
“`

test:

#include <iostream>#include "timer.h"using std::cin;using std::cout;using std::endl;using std::string;using namespace TIC_TOC;// f(n) = 0, n = 1// f(n) = 1, n = 2// f(n) = f(n-1) + f(n-2), n > 2int fabonaci(int n) {    clock_f();  // it takes 50 clocks (0.050ms)    if (n == 1) return 0;    else if (n == 2) return 1;    else return fabonaci(n-1) + fabonaci(n-2);}// f(n) = 1, n = 1// f(n) = n * f(n-1), n > 1int factorial(int n) {    clock_f();    if (n == 1) return 1;    return n*factorial(n-1);}timer_controller T;  // create a timer_controllerint number[5];  // input for testint count;  // count the lines(tic -> toc)void testTicToc(int n, string function) {    count = 0;    cin >> number[0] >> number[1] >> number[2]    >> number[3] >> number[4];    for (int i = 0; i < 5; i++) {        T[n].tic_f(count++);  // tic for starting time        cout << "input: " << number[i] << ", result: ";        if (function == "fabonaci")            cout << fabonaci(number[i]) << endl;        else if (function == "factorial")            cout << factorial(number[i]) << endl;        T[n].toc_f(count);  // toc for ending time    }    T[n].tictoc(stdout);  // print out the line-time information}int main(void) {    T.create(2);    testTicToc(0, "fabonaci");    testTicToc(1, "factorial");    T.display(stdout);  // print out the total time of each timer    return 0;}

My answer:

#include <time.h>#include <iomanip>#include <stdio.h>#include <iostream>static int _clock = 0;inline void clock_f() {    for (int i = 0; i != 50; i++) {        _clock++;    }}inline double getTime() {    return 0.001 * _clock;}#define MAX_TIMER 5// namespace TIC_TOCnamespace TIC_TOC {class timer {private:    double times[5];public:    timer() {        for (int i = 0; i != 5; i++) {            times[i] = 0;        }    }    double getAllRdtsc() {        double totalTime = 0;        for (int i = 0; i != 5; i++) {            totalTime += times[i];        }        return totalTime;    }    void tic_f(int i) {        times[i] = getTime();    }    void toc_f(int i) {        times[i - 1] = getTime() - times[i - 1];    }    void tictoc(FILE* one) {        std::cout << "line 0 - 1: " << std::fixed        << std::setprecision(3) << times[0] << "ms" << std::endl;        std::cout << "line 1 - 2: " << times[1] << "ms" << std::endl;        std::cout << "line 2 - 3: " << times[2] << "ms" << std::endl;        std::cout << "line 3 - 4: " << times[3] << "ms" << std::endl;        std::cout << "line 4 - 5: " << times[4] << "ms" << std::endl;    }};class timer_controller {    timer timePiece[MAX_TIMER];public:    timer_controller() {    }     void create(int i) {         while (i != 0) {             timePiece[i] = timer();             i--;         }     }     timer& operator[](int i) {         return timePiece[i];     }    void display(FILE* one) {        std::cout << "#Timer_1: " <<        timePiece[0].getAllRdtsc() << "ms" << std::endl;        std::cout << "#Timer_2: " <<        timePiece[1].getAllRdtsc() << "ms" << std::endl;    }};}  // namespace TIC_TOC

分析:

  • 这里其实是用一个循环来确定时间。每执行一次循环认为耗时一个_clock。这样就可以理解我们要如何确定时间差了。

答案:

#ifndef TIMER_H#define TIMER_H#include <stdio.h>#include <memory.h>#define MAX_LINE 10000#define MAX_TIMER 5#define _CLOCKS_PER_SEC 1000static int _clock = 0;inline void clock_f() { _clock += 50; }inline double getTime() { return 1.0 * _clock / _CLOCKS_PER_SEC; }namespace TIC_TOC {class timer {    double _timer[MAX_LINE];  // time for each line    int _fromLine[MAX_LINE];  // line starting point    int _lastLineNum;  // mark the ending line    double _lastRdtsc;  // last time using getime()    double _totalRdtsc;  // time for all lines    public:        timer() {            memset(_timer, 0, sizeof(_timer));            memset(_fromLine, 0, sizeof(_fromLine));            _lastLineNum = -1;            _lastRdtsc = _totalRdtsc = 0;        }        double getAllRdtsc() { return _totalRdtsc; }        void tic_f(int line) {            _lastLineNum = line;            _lastRdtsc = getTime();        }        void toc_f(int line) {            double t = getTime();            _fromLine[line] = _lastLineNum;            _timer[line] += t - _lastRdtsc;            _totalRdtsc += _timer[line];            _lastLineNum = line;            _lastRdtsc = t;        }        void tictoc(FILE *out) {        // 给出了一个文件指针。            for (int i = 0; i < MAX_LINE; i++)                if (_timer[i] != 0)                    fprintf(out, "line %d - %d: %.3fms\n",                        _fromLine[i], i, _timer[i]);            fflush(out);        }};class timer_controller {    int count;  // count timers    timer timePiece[MAX_TIMER];  // 5 timers at most for each timer_controller    public:        timer_controller() { count = 0; }        void create(int input) { count = input; }        timer& operator[](const int& input) { return timePiece[input]; }        void display(FILE *out) {            for (int i = 0; i < count; i++)                fprintf(out, "#Timer_%d: %.3fms\n",                    i+1, timePiece[i].getAllRdtsc());            fflush(out);        }};}  // namespace TIC_TOC#endif

补充:


函数名: fflush
功 能: 清除读写缓冲区,需要立即把输出缓冲区的数据进行物理写入时
头文件:stdio.h
原型:int fflush(FILE *stream)
其中stream是要冲洗的流

int fprintf (FILE* stream, const char*format, [argument])
FILE*stream:文件指针
const char* format:输出格式
[argument]:附加参数列表
%d, %i 十进制有符号整数
%u 十进制无符号整数
%f 浮点数
%s 字符串
%c 单个字符
%p指针的值
%e, %E 指数形式的浮点数
%x无符号以小写十六进制表示的整数
%X 无符号以大写十六进制表示的整数
%o 无符号以八进制表示的整数
%g 自动选择合适的表示法[1]

0 0
原创粉丝点击