473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

clock_gettime(CLOCK_MONOTONIC, struct timespec*) equivalent for c#

Hello folks!

I am new to c# and want to port a c++ library to c#. I need an accurate
timesource which has at least millisecond resolution. A clock, which
counts the seconds/milliseconds since booting windows would be great. A
clock which may jump in time during runtime would lead to false result.
Under Linux and c++ I used the function clock_gettime(CLOCK_MONOTONIC,
struct timespec*).

Any suggestions?

CU,
Daniel
Nov 8 '07 #1
4 8376
Hello again,
after further searching on the net I found the class
System.Diagnostics.StopWatch. I can start that stopwatch when my
library/application starts, and use the Elapsed Property. Any doubts
that this is a good idea? Or ar there better ways?

CU,
Daniel

Daniel Kay schrieb:
Hello folks!

I am new to c# and want to port a c++ library to c#. I need an accurate
timesource which has at least millisecond resolution. A clock, which
counts the seconds/milliseconds since booting windows would be great. A
clock which may jump in time during runtime would lead to false result.
Under Linux and c++ I used the function clock_gettime(CLOCK_MONOTONIC,
struct timespec*).

Any suggestions?
Nov 8 '07 #2
On 2007-11-08 05:39:01 -0800, Daniel Kay <da*********@arcor.desaid:
Hello again,
after further searching on the net I found the class
System.Diagnostics.StopWatch. I can start that stopwatch when my
library/application starts, and use the Elapsed Property. Any doubts
that this is a good idea? Or ar there better ways?
A Stopwatch instance would be fine. You could also just use
DateTime.Now to obtain the current time, subtracting from that a
previously acquired time to obtain a TimeSpan value. I would say those
are the two most common time-keeping techniques.

You should keep in mind that even though Stopwatch and DateTime
ostensibly provide millisecond precision, they aren't actually accurate
to the nearest millisecond. The nature of Windows thread scheduling
will prevent you from getting timing information more accurate than to
tens of milliseconds.

Finally, note that there are actually timer classes (at least three
that I'm aware of). Depending on what the original code did with the
time information, it may be better to use a timer. In particular, you
probably would not want to poll the current time on a very frequent
basis (e.g. in a tight loop).

Pete

Nov 8 '07 #3
Peter Duniho schrieb:
On 2007-11-08 05:39:01 -0800, Daniel Kay <da*********@arcor.desaid:
>Hello again,
after further searching on the net I found the class
System.Diagnostics.StopWatch. I can start that stopwatch when my
library/application starts, and use the Elapsed Property. Any doubts
that this is a good idea? Or ar there better ways?

A Stopwatch instance would be fine. You could also just use
DateTime.Now to obtain the current time, subtracting from that a
previously acquired time to obtain a TimeSpan value. I would say those
are the two most common time-keeping techniques.

You should keep in mind that even though Stopwatch and DateTime
ostensibly provide millisecond precision, they aren't actually accurate
to the nearest millisecond. The nature of Windows thread scheduling
will prevent you from getting timing information more accurate than to
tens of milliseconds.

Finally, note that there are actually timer classes (at least three that
I'm aware of). Depending on what the original code did with the time
information, it may be better to use a timer. In particular, you
probably would not want to poll the current time on a very frequent
basis (e.g. in a tight loop).
Thanks for your reply. The library I like to port receives data from the
serial port in a message based protocol. I save every received byte in
an object, which retrieves the current timestamp. When a message is
completly received I check if any abnormal delays occured during
transmission by calculating the timespan between the first and last
received byte. Knowing the transfer speed I can decide whether to accept
and process the message or not. If for instance a message took 44ms to
transmit, but I calculated an expected time of 12ms, I ignore the
message. I am not so strict on the thresholds, because the UART may
delay the interrupt if the FIFO isn't totally filled up. But at the end
I don't want to use the systemtime, because the user may change the time
while I receive a message and then messages would be dropped which
weren't received delayed.

The serialport is usally set at 19200bps and data isn't received all the
time. So I do not expect that polling the stopwatch time does use to
much system resources. Thanks again for your comments. It seems that
it's not the worst solution I have found. :-)

CU,
Daniel

CU,
Daniel
Nov 8 '07 #4
On 2007-11-08 12:44:58 -0800, Daniel Kay <da*********@arcor.desaid:
[...]
The serialport is usally set at 19200bps and data isn't received all
the time. So I do not expect that polling the stopwatch time does use
to much system resources. Thanks again for your comments. It seems that
it's not the worst solution I have found. :-)
No, it should work pretty well for your purposes. Do keep in mind that
in Windows tracking the time this way isn't going to provide a reliable
distinction between a 12ms delay and a 44ms delay. The 32ms difference
is barely within the granularity the thread scheduler allows; a delay
in transmission that small could easily be due simply to the fact that
the thread receiving the data didn't get run any sooner than that.

Also, rather than getting a timestamp for every byte received, you may
find it's more efficient to just start the Stopwatch at the first byte,
and then check the elapsed time at the last byte.

Other than that, I think that Stopwatch would probably be fine for what
you're talking about.

Pete

Nov 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DiskMan | last post by:
System: Redhat 7.2 Kernel-2.6.11.8 GCC-3.4.3 CCC-6.5.9 Binutils-2.15 Make-3.80 GTK/GLIB-2.6.7 For some reason my Linux box is suddenly having issues trying to read ;
6
by: bg_ie | last post by:
Hi, I wish to write a C program which obtains the system time and hence uses this time to print out its ntp equivalent. Am I right in saying that the following is correct for the seconds part...
4
by: legrape | last post by:
I am porting a piece of C code to 64bit on Linux. I am using 64bit integers. It is a floating point intensive code and when I compile (gcc) on 64 bit machine, I don't see any runtime improvement...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.