View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-25-2007, 08:44 PM   #1
Registered: Jul 2004
Location: Lubbock, Texas
Distribution: Slackware 12.0 with Beryl 0.2.1
Posts: 334

Rep: Reputation: 30
Still need help with timer

I posted a thread earlier, then said I figured it out. Well, no I didn't lol.

I need a timer to time how long a function takes to complete. My teacher thinks that milliseconds is enough time, but I think that the function completes in < 1 millisecond. So I need to find a function that returns time in micro or nano seconds. I'm doing this in C, on Windows (usually I'd say Linux, but my teacher won't accept Linux executables.)
Old 04-25-2007, 08:50 PM   #2
Registered: Jul 2004
Location: Lubbock, Texas
Distribution: Slackware 12.0 with Beryl 0.2.1
Posts: 334

Original Poster
Rep: Reputation: 30
This is my code:
/* CS 2350 Spring 2007											 */
/* C section for Assignment 5                                    */
/************************ include files **************************/
//65-90 97-122
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//#define CLOCKS_PER_SEC 1000

//External functions/variables
extern "C" void lexsort(char*,char*);
//Global functions/variables
int COUNT;
int ITER  = 0;

int main(void)
	scanf("%d", &COUNT);
	//random-related statements/variables
	int ran;

    char* input = new char[COUNT+1];
	char* output = new char[COUNT+1];	
	output[COUNT] = NULL;
	input[COUNT] = NULL;

	//time-related variables
	struct timeval before, after;
	double diff;
	int x = 0;

	while(x < COUNT)
		ran = rand() % 57 + 65;
		if(ran > 90 && ran < 122)
			input[x++] = ran;

	printf("input = %s\n\n", input);
	//implement lexsort function

	gettimeofday(&before, 0);
	lexsort(input, output);
	gettimeofday(&after, 0);
	//diff = after.tv_nsec - before.tv_nsec;
	//output results
	printf("output = %s\n", output);
	printf("Time to implement: %d", after.tv_usec);
	printf("\nbefore = %d", before.tv_usec);

	return 0;
Now, I really don't think my computer is fast enough to do a function in under 1 microsecond, (2.8 P4), but if thats the case, then I need a nanosecond timer.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multimedia timer (SMP friendly timer) bigqueso Linux - Kernel 0 03-15-2007 03:49 PM
About timer.... help rajsun Programming 3 05-24-2005 06:06 AM
timer in c?? AcidHell2 Programming 9 08-12-2004 01:01 AM
First timer, help!!!!! jcniest5 Linux - Newbie 7 02-24-2004 08:32 AM
timer in C timmy334 Programming 10 11-18-2003 11:40 AM

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration