How to make a nanosecond timer

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
5 posts Page 1 of 1
Contributors
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

This is a tutorial that shows you how to make a nanosecond timer.

A nanosecond is one billionth of a second.
.000000001 or 10^-9

_________________________________________________________________________________
Form
_________________________________________________________________________________

Create a new project. Add the following controls:
Button1 (Start)
Button2 (Reset)
Label1
Timer1
Form.PNG
_________________________________________________________________________________
Code
_________________________________________________________________________________

Imports:
Code: Select all
Imports System.Diagnostics.Stopwatch
Add this code to the project:
Code: Select all
    Dim StopWatch As Stopwatch = New System.Diagnostics.Stopwatch 'Creates the variable StopWatch, the main component of this project


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Start" Then 'If Button1's text is "Start", then...
            Button1.Text = "Pause" 'Changes Button1's text to "Pause"
            StopWatch.Start() 'Starts Stopwatch
        Else 'Else
            Button1.Text = "Start" 'Changes Button1's text to "Start
            StopWatch.Stop() 'Pauses Stopwatch
        End If

        Label1.Text = (1 ^ 9 / StopWatch.Frequency * StopWatch.ElapsedTicks) / 1 ^ 9 'Gets the ticks from the stopwatch, then converts to nanoseconds

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = (1 ^ 9 / StopWatch.Frequency * StopWatch.ElapsedTicks) / 1 ^ 9 'Gets the ticks from the stopwatch, then converts to nanoseconds
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        StopWatch.Reset() 'Resets the stopwatch to 0
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True 'Enables Timer1
        Timer1.Interval = 1 'Changes tick interval to 1 millisecond
    End Sub
_________________________________________________________________________________
Downloadable Source
_________________________________________________________________________________
Nanosecond Timer Source.zip
You do not have the required permissions to view the files attached to this post.
http://www.megaapps.tk/
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: How to make a nanosecond timer
mandai
You could also find the nanosecond count with:
(1000000000 * StopWatch.ElapsedTicks / Stopwatch.Frequency)
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

They both do the same thing, so I guess you could use that too.
http://www.megaapps.tk/
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: How to make a nanosecond timer
mandai
I'm no mathematician, but there does seem to be a different level of accuracy between these methods.
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

Re: How to make a nanosecond timer
Bogoh67
yocto second?
5 posts Page 1 of 1
Return to “Tutorials”