How to Measure Milliseconds in ANSI C

How to Measure Milliseconds in ANSI C thumbnail
Time can be a slippery concept to ANSI C.

The American National Standards Institute (ANSI) provides standardized coding guidelines that enhances software portability, but in doing so some of the more robust features of the C language are lost. Producing a perfect determination of time to the millisecond is never easy (due to the nature of digital computing); developing a solution using strict ANSI C standards is impossible. However, a savvy ANSI C programmer can make use of the clock() function and simple math to calculate the time to the precision of milliseconds.

Things You'll Need

  • ANSI C compiler
  • Begin time
  • End time
Show More

Instructions

    • 1

      Open a plain text editor (such as Notepad). Copy the code below and save your file.

      #include <time.h>

      double elapsed; // in milliseconds

      clock_t start, end;

      start = clock();

      /* do some work */

      end = clock();

      elapsed = ((double) (end - start) * 1000) / CLOCKS_PER_SEC;

    • 2

      Compile your code. If there are any errors, debug, correct and compile again.

    • 3

      When your compiler produces a return code of zero, execute your program by double clicking the newly produced EXE file through Explorer or by running the executable via the command line.

Tips & Warnings

  • If you don't have a compiler, one suggestion is to use GCC (Gnu Compiler Collection). It's free, easy to use and supported and developed under open source.

  • Save your file with an extension of c (such as millisecond.c). Most compilers won't work without the appropriate extension.

  • Many compilers require an input parameter to compile a program to ANSI standards. Check the documentation of your compiler program for further details.

Related Searches:

References

Resources

  • Photo Credit digital clock image by JoLin from Fotolia.com

Comments

You May Also Like

  • How to Calculate Milliseconds

    Metric prefixes are applied to modify the International System of Units (SI). They are a convenient way to express very large or...

  • How to Read a File in ANSI C

    One of the first things you learn as a C programmer is how to read data from a file. Though it was...

  • ANSI C Data Types

    ANSI C Data Types. The American National Standards Institute (ANSI) standard for the C computer programming language remains true to the minimalist...

  • How to Convert Milliseconds to a Date

    A millisecond equals exactly one thousandth of a second. While the time unit isn't applicable in most of our daily lives, milliseconds...

  • How to Read a Hex File With ANSI C

    Knowing how to read hexadecimal (hex) values in a file using the C programming language can save you time when you need...

  • How to Convert Milliseconds to Seconds in Java

    The Java programming language thinks of time primarily in terms of milliseconds, or thousandths of a second. Should you ever wish to...

  • 1998 ANSI B30.5 Qualifications

    1998 ANSI B30.5 Qualifications. The American National Standards Institute (ANSI) is an organization that provides accreditation to a variety of regulations for...

  • How to Write a C++ Program to Simulate a Digital Clock

    The standard C++ library includes a number of functions and structures to assist programmers in dealing with time in their applications. In...

  • ANSI Encoding in C#

    ANSI was replaced in Visual C# for good reason: it translates poorly to other languages. Unless you know the code page number...

  • How to Convert ANSI Codes

    ANSI characters, also known as Windows-1252, are a set of 217 codes adopted by the ANSI Standard Organization. These codes include a...

Related Ads

Featured