Two implementation methods of time_t and clock_t of Timers (recommended) _c language

Source: Internet
Author: User

To calculate the running time for your initial phase space search algorithm, try the following ways of using the time_t type

#include <stdlib.h>
#include <iostream>
#include <time.h>
#include "StateFunctions.h "

using namespace std;

int main (int argc, char** argv)
{
  time_t start, finish;

  Time (&start);

  Statefunctions testobj (in);
  Testobj. TEST ();
  Testobj. TEST ();
  Testobj. Fillrandomdets ();
  Testobj.evolute (1000, 0.9);

  cout << "--------------------------------------------" << Endl;
  Time (&finish);
  Double duration = difftime (finish, start);
  cout << "--> Time:" << duration << "s" << Endl;
  cout << "--------------------------------------------" << Endl;

  return 0;
}

This realization method can correctly calculate the core part of the algorithm consumes 234 seconds of walltime. The attempt to use the clock_t type before this is

#include <iostream>
#include <time.h>
#include "StateFunctions.h"

using namespace std;

int main (int argc, char** argv)
{
	clock_t start, finish;
	start = Clock ();

	Statefunctions testobj (in);
	Testobj. TEST ();
	Testobj. TEST ();
	Testobj. Fillrandomdets ();
	Testobj.evolute (1000, 0.9);

	cout << "--------------------------------------------" << Endl;
	finish = Clock ();
	Double duration = (double) (finish-start)/clocks_per_sec;
	cout << "--> Time:" << duration << "s" << Endl;
	cout << "--------------------------------------------" << Endl;

	return 0;
}

This code has been running for only 11 seconds, obviously not. The reason for this result is not yet clear, perhaps because the algorithm in the implementation of the process of frequent calls to other external programs to obtain some calculation results.

The above is a small series for you to bring the timer time_t and clock_t two ways to achieve (recommended) all content, I hope that we support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.