c++ - How To Return Elapsed Time From steady_clock as a Primitive Data Type (double)


Question: 

First and foremost, let me say that I just starting using this library yesterday, so my understanding of it is still fairly basic. I'm trying to capture the FPS of a vision processing program I'm creating and output it to a screen using the chrono library. In my case, I need to cast the elapsed time taken after I start a steady_clock to a double (or some other numerical typedef I could treat like a double). I looked through reference documentation and tried working with the duration_cast and time_point_cast functions, but neither of those seem to be what I'm looking for.

My question is; is there any way to simply cast the numerical value of a clock's current state in seconds to a primitive data type?

Any help would be appreciated.




2 Answers: 

Like this:

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

int main()
{
  using namespace std::literals;

  // measure time now
  auto start = std::chrono::system_clock::now();

  // wait some time
  std::this_thread::sleep_for(1s);

  // measure time again
  auto end = std::chrono::system_clock::now();

  // define a double-precision representation of seconds
  using fsecs = std::chrono::duration<double, std::chrono::seconds::period>;

  // convert from clock's duration type
  auto as_fseconds = std::chrono::duration_cast<fsecs>(end - start);

  // display as decimal seconds
  std::cout << "duration was " << as_fseconds.count() << "s\n";
}

example output:

duration was 1.00006s
 

You could do it using the duration::count function.

For example you could get the duration in the number of milliseconds, and then divide the count by 1000.0 to get the number of seconds as a double.

 

More Articles


How do I cross-compile a Linux kernel to a MIPS little endian host?

The kernel in question is 2.6.18. If I callmake ARCH=mips CROSS_COMPILE=mipsel-linux- menuconfigthere will be only the option to build a big endian kernel in the menu. If I use ARCH=mipsel, it will complain about not having an arch/mipsel dir.How's this done?

android - Can't find class [org/drinkless/td/libcore/telegram/TdApi$Object]

I am getting this error "Can't find class [org/drinkless/td/libcore/telegram/TdApi$Object]" but I haven't used this class anywhere in my project.I haven't used that class anywhere.But i am getting the above errorCan anyone please let me know how to solve the above error.

java - Location mocking - google map detects the movement but my own application not triggering location change

I am facing a strange issue. I modified official mock provider source code provided by google to mock some route for my application.Using this code. mockLocation.setElapsedRealtimeNanos(elapsedTimeNanos); mockLocation.setTime(currentTime); // Set the loc


python - Heap that supports modification of its elements?

Here is my scenario. I want to implement A* (in Python) without having to resort to linear-time min or in operations. I need a heap to be able to efficiently get the lowest weight item. My immediate response was 'Easy! I'll use heapq!' Then I discovered that life is rarely as simple as we would like

java - gRPC: Generate certificateChainFile and privateKeyFile to make TLS/SSL work

I want to make tls/ssl connection between client and server(Not mutual, one way). This is my setup:Server: Server server = ServerBuilder.forPort(8443) // Enable TLS .useTransportSecurity(certChainFile, privateKeyFile) .addService(new GreetingServiceImpl()) .build();Cli

hashtable - Powershell 2 and .NET: Optimize for extremely large hash tables?

I am dabbling in Powershell and completely new to .NET.I am running a PS script that starts with an empty hash table. The hash table will grow to at least 15,000 to 20,000 entries. Keys of the hash table will be email addresses in string form, and values will be booleans. (I simply need to track whe


Missing plugin for GStreamer for Android SDK

I changed the stream url in the included Tutorial 5 (a basic media player) to a h.264/mp3 media stream (from its original ogv stream) and it started complaining about some missing plugins.After doing some googling I found Prajnashi's gst-ffmpeg plugin for Android https://github.com/prajnashi/gst-ffm

powershell - How to add key/value pair at the end of hash table?

I am trying to calculate code count using PowerShell script.I found a script on the Internet and am trying to add the total line at the end.I have added the column$CountHash.Add("Total", $Total)at the end.Param( [string]$path, [string]$outputFile, [string]$include = "*.*", [string]

.net - HMACSHA256.ComputeHash - Unexpected Result

I'm trying to generate a signature in VB.NET using the following vendor documentation as a reference guide:https://shuttle.support.signiant.com/customer/en/portal/articles/2807676-media-shuttle-metadata-developer-s-guide#AppendixAThey also provide this sample JS code:https://github.com/Signiant/medi

r - adding two column of a data where col1 contains date and col2 contains days

I have a data frame in which i have two columns date and days and i want to add date column with days and show the result in other column data frame-1col date is in format of mm/dd/yyyy formatdate days3/2/2019 83/5/2019 43/6/2019 43/21/2019 33/25/2019 7and i want my output like t