:lol: hello
i want to show the time in a certain program in part of seconds but i don't know,can you help me in solve this problem?
thanks

Dani AI

Generated

asked how to show time "in part of seconds" in a C++ console program. and pointed to std::clock()—that can be useful, but note it reports CPU time used by the process, not wall‑clock (real) elapsed time. For elapsed intervals and clean fractional seconds output, prefer the C++11 <chrono> facilities (they are portable and less error‑prone).

Here is a simple, ready-to-run example that measures elapsed wall time and prints seconds with fractional part:

#include <iostream>
#include <chrono>
#include <iomanip>
#include <thread>

int main() {
    using namespace std::chrono;
    auto start = steady_clock::now();

    // work to measure (example)
    std::this_thread::sleep_for(milliseconds(123));

    auto end = steady_clock::now();
    duration<double> secs = end - start;
    std::cout << "Elapsed: " << std::fixed << std::setprecision(6)
              << secs.count() << " s\n";
}

Notes and practical tips:

  • Use steady_clock for interval timing because it is monotonic (no jumps if the system clock changes). duration<double> gives seconds as a fractional double; control visible decimals with std::setprecision.
  • If you really need CPU-time (time the processor spent on your code) then std::clock() is appropriate—that is what and were pointing toward.
  • For very high-resolution or platform-specific needs: POSIX systems can use clock_gettime(CLOCK_MONOTONIC, ...), and older Windows code often uses QueryPerformanceCounter; modern std::chrono implementations commonly use those under the hood.
  • For a live updating display, sample at a reasonable interval (e.g., 30–100 ms) and print with a carriage return ('\r') plus std::flush to avoid busy-waiting.

Recommended Answers

All 5 Replies

The function clock() in time.h or ctime returns the number of ticks since "process start". A tick is about a millisecond.

:lol: hello
i want to show the time in a certain program in part of seconds but i don't know,can you help me in solve this problem?
thanks

What is the language you are using? Is it a console program or a GUI program?

What is the language you are using? Is it a console program or a GUI program?

i use C++

console

Then the function std::clock() and the class std::clock_t can be used.

#include <ctime>
std::clock_t start_time = std::clock();
//Do something
std::clock_t time_passed = std::clock() - start_time;
std::cout << "time = " << time_passed;

edit:
convert this to seconds by using the CLOCKS_PER_SEC macro. you get the above in clock ticks. It is approximately a millisecond.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.