LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 02-15-2012, 06:29 AM   #1
connect2janu
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Rep: Reputation: Disabled
what is the header file to find time cosumed for sorting a given array[sorted/unsort


hi all
i want find time consumed in seconds/minutes/ hours for sorting a given array
i have quick sort programme,in that i added <time.h> header file in starting of the programme, but when i compile the programme,i am getting as sorted array as result but its not giving exact time consumed for sorting given array .

can you point out my mistake
or
which header file do i need to use?

if you want more i can attach my programme.
i want run above the programme[quick sort] in fedora operating system[any version]

Last edited by connect2janu; 02-15-2012 at 10:41 PM.
 
Old 02-15-2012, 08:47 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,554
Blog Entries: 3

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Are you interested in CPU time used, or wall clock time used?

To measure the time taken by a program, you can use the time command. For example,
Code:
time ./my-program argument(s)...
The time command will run ./my-program , and after it exits, print output similar to
Code:
real	0m0.005s
user	0m0.002s
sys	0m0.001s
to standard error. The real is the wall clock time taken by running the program. It includes the time taken to load the program and any dynamic libraries, too. The user is the amount of time spent by the CPU running your userspace code, and sys is the amount of time spent by the CPU running kernel code.

It is usually a better idea to measure and print the time in the program itself. For example,
Code:
#define _POSIX_C_SOURCE 200809L
#include <time.h>

static inline struct timespec wallclock_now(void)
{
    struct timespec now;
    if (clock_gettime(CLOCK_REALTIME, &now)) {
        now.tv_sec = 0;
        now.tv_usec = 0;
    }
    return now;
}

static inline double wallclock_since(const struct timespec since)
{
    struct timespec now;
    if (clock_gettime(CLOCK_REALTIME, &now))
        return 0.0;
    return (double)now.tv_sec - (double)since.tv_sec
         + (double)(now.tv_nsec - since.tv_nsec) / 1000000000.0;
}

static inline double cpuclock_now(void)
{
    struct timespec now;
    if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now)) {
        now.tv_sec = 0;
        now.tv_usec = 0;
    }
    return now;
}

static inline double cpuclock_since(const struct timespec since)
{
    struct timespec now;
    if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now))
        return 0.0;
    return (double)now.tv_sec - (double)since.tv_sec
         + (double)(now.tv_nsec - since.tv_nsec) / 1000000000.0;
}


static inline double threadclock_now(void)
{
    struct timespec now;
    if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now)) {
        now.tv_sec = 0;
        now.tv_usec = 0;
    }
    return now;
}

static inline double threadclock_since(const struct timespec since)
{
    struct timespec now;
    if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now))
        return 0.0;
    return (double)now.tv_sec - (double)since.tv_sec
         + (double)(now.tv_nsec - since.tv_nsec) / 1000000000.0;
}
To use the above, you grab the current time just before calling the function(s) to be benchmarked,
Code:
    struct timespec  start;
    double           seconds;

    start = wallclock_now();
and afterwards, get the seconds elapsed using the corresponding function,
Code:
    seconds = wallclock_since(start);
Don't mix the clocks, though.

On many architectures the CPU (and thread) clocks are based on the counters the CPU provides; on x86, the TSC (time stamp counter) register is used. See man clock_gettime for caveats on determining the process or thread CPU time used; in some cases, if the task migrates to another CPU, the results may fluctuate a bit. (I do believe the Linux kernel tries hard to mitigate such errors, but be aware of those issues.)

As far as I know, the wall clock time has no such limitations. (Except that jumping the time, e.g. using ntpdate -b may cause the time to be incorrectly reported. Local changes, i.e. daylight savings time, or changing the timezone, do not affect the wall clock measurement.)
 
Old 02-15-2012, 10:25 PM   #3
connect2janu
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
hello Nominal Animal
thanks for reply


you are saying that to get time consumed for sorting a given array i need to do below things

step 1)cc quick_sort
step 2)./a.out
step 3) time ./my_programme[ ex: time quick_sort)
right??

k here i am attaching quick sort programme
plz if possible point out the errors/ correct the programme
Attached Files
File Type: txt quick _sort.txt (997 Bytes, 4 views)

Last edited by connect2janu; 02-15-2012 at 10:27 PM.
 
Old 02-16-2012, 05:48 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,554
Blog Entries: 3

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Quote:
Originally Posted by connect2janu View Post
you are saying that to get time consumed for sorting a given array i need to do below things

step 1)cc quick_sort
step 2)./a.out
step 3) time ./my_programme
No, but close:
  1. Compile using e.g.
    gcc quick_sort.c -Wall -o quick_sort
  2. Run under time , e.g.
    time ./quick_sort

The -Wall option tells your compiler to warn about all problems it sees with the code.

The code you posted is full of typos. C is case sensitive, Void and void are two totally different things. “ and " are different characters. Even if you fix all those, your program logic does not work. Finally, the program contains the timing instructions (albeit inaccurate), so it will do the timing measurements itself.

It is obvious you wrote that file in a word processor (MS Word, LibreOffice Writer, or some other). Do not do that. Use a text editor instead. Word processors tend to try to help you write better human-readable text, not source code.

It is clear to me now that this is coursework, and you haven't even tried to compile your program. I'm quite disappointed in you.
 
Old 02-18-2012, 05:55 AM   #5
connect2janu
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Nominal Animal
hi sir
-sorry for the code which was written as in word
-i tried, but -my aim is different see in that time[real,user,sys] is giving how much time taken to run the programme,but i want time consumed[taken] to sort a given array[it may be sorted or unsorted array]

here i am adding a code in text file,but when i compile sometime i am getting as segmentation fault?

but some time its working

can you check this
i did following steps:
step1)gcc tjohn.c
step2)./a.out
step3)got output

-last thing i did what you told:Compile using e.g.
gcc quick_sort.c -Wall -o quick_sort
result:gcc: unrecognized option '-wall'
i am getting confuse in that programme what i am doing wrong,plz help me
thanks
Attached Files
File Type: txt tjohn.txt (1.0 KB, 2 views)
 
Old 02-18-2012, 10:42 AM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,554
Blog Entries: 3

Rep: Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816Reputation: 816
Quote:
Originally Posted by connect2janu View Post
-sorry for the code which was written as in word
-i tried, but -my aim is different see in that time[real,user,sys] is giving how much time taken to run the programme,but i want time consumed[taken] to sort a given array[it may be sorted or unsorted array]
Okay; that means you need to measure the user cpu time taken by the sort function only.

Quote:
Originally Posted by connect2janu View Post
-last thing i did what you told:Compile using e.g.
gcc quick_sort.c -Wall -o quick_sort
result:gcc: unrecognized option '-wall'
You used -wall instead of -Wall , the W must be in uppercase.

Remember that for computers, uppercase and lowercase letters are totally different things. There are features like certain filesystems that are case insensitive, but they are the exceptions, not the rule.

When I compile your program (after renaming it to tjohn.c) using gcc tjohn.c -Wall -o tjohn I get the following warnings:
Code:
tjohn.c:42:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
tjohn.c: In function ‘partition’:
tjohn.c:29:1: warning: control reaches end of non-void function [-Wreturn-type]
tjohn.c: In function ‘main’:
tjohn.c:58:8: warning: ‘CLK_TCK’ is used uninitialized in this function [-Wuninitialized]
The first one is because C really expects the main function to be declared int main(). You should fix that, and end your main with return 0; (assuming normal, successful exit), or return EXIT_SUCCESS; if you want to be as portable as possible.

The second one is because sometimes (whenever i<j) your partition function falls out of the if clauses, and exits without returning anything.

The third one is because in Linux, there is no such constant as CLK_TCK. You declared it as a variable in your program, but you don't specify any value. (If it gets set to zero, you'll get a division by zero error.) To convert the result from clock() you need to use the CLOCKS_PER_SEC constant instead.

I don't use Windows myself, but the Microsoft references indicate you should use CLOCKS_PER_SEC with clock() there too.

However, clock() function is very imprecise; it only changes sysconf(_SC_CLK_TCK) times per second in Linux, CLK_TCK times per second in Windows. Since CLOCKS_PER_SEC is defined by POSIX to always be 1000000, it is increased by several thousand every time it changes; it just changes rather rarely. You get much more precise results by using getrusage() if possible.

Because your for loops are (i=0;i<=n;i++) you will actually read and process one more value than you ask for. To loop over the first n*integers starting with zero, you use (i=0;i<n;i++). The first i will then be zero, and the last n-1. (Just think about it. 0 1 2 3 4 has five integers, doesn't it?)

Your quick_sort function only handles the case where low<high. This is not a bug, and actually shields you from another bug: you don't check if the first number, n, the user first supplies, is sane.

(In a general quicksort function it would be better to add an else case that does the exact same thing, but with low and high swapped, so it would do the correct thing even if you supplied them in the wrong order. In your program, you do not need to do that; I'd only add a comment "Since low<high in this program, the case low>high is not handled." into the quick_sort function, mostly to help you remember if you ever choose to reuse the function in some other program.)

Your partition function fails to do the partitioning. It does a single swap in one case, and copies one element to itself in the other case. (It would not even work as a swap, if j could ever be greater than i).

I could write the function and fix all the other bugs I've mentioned above, but that would be a disservice to you. Besides, I hate cheaters and would never knowingly write anybodys coursework for them; I'd rather gnaw my arms off. (Not because it's wrong, but because it rewards and promotes a type of behaviour that absolutely infuriates me. I really hate cheaters and exploiters, and I'm not talking about schoolwork only; I mean in general.)

Learning to create something new yourself is and should be a great experience. It should boost your self esteem, and open up new possibilities for expression and developing new problem solving skills. It is often hard. Especially at the very beginning it may be very hard, because not only you have to learn something new, but you often must first learn a new way of thinking. It seems that this "pain" -- really, concentration and effort -- is vital to the learning process in humans. If it was easy, you would not have to learn it at all.

I myself am here because I am addicted to solving technical problems. I, and most members here, won't write or fix your program for you. We usually need to see your efforts first, because without effort, there is no learning; when there is no learning, we get no enjoyment in helping others. (You can always find a mentor to help you learn, but you won't find anyone who'll do your homework for you for free without any form of compensation.)

In this particular case I am a bit frustrated, because I can see you are unaware of the fundamentals of programming. I don't know if you are self-taught and have just accidentally overlooked the introduction, or if you were asleep when they were talked about in the class, or if you have a sub-par mentor or teacher who has for some reason skipped the introduction. It is the part which describes the basic terms and approach; it is like the foundations of a building. Without it, stuff just sinks into the ground, and you may get severe problems later on. It feels like washing a car just before a long trip in rainy and windy weather.

My own background is in physics, so I like to see the terms and assumptions clearly before building anything on them. In most introductions to C programming this stuff is actually interspersed into the first few chapters, probably to get you started as soon as possible without having a few pages of dry text to read first. I guess it works better that way for most people. However, when you jump into algorithms (like quick sort here), you need to have the background or introduction solidly beneath your feet first; otherwise, it's going to be very hard and frustrating. As an example, check out the C tutorial at cprogramming.com.

If you are following some course instead, I recommend you read the introduction and first few chapters first to see if there are concepts and themes that may "jolt" your thinking. Or perhaps review the previous courses. Do not bother remembering any details like function prototypes, just try to understand the terms and descriptions and explanations in the text. The syntax and details can be checked later on.

My own first commercial gig as a programmer was more than half a lifetime ago, and I've never needed to rely on my memory for the details. I always have a spare window open to peruse the man pages and documentation, to check and verify. And I do. What I do have, is a very deep and thorough understanding. (I'm very proud, perhaps a bit too proud, of my skills.) A good memory may make you a faster coder, but it is not at all important. But, without understanding, you'll never get beyond the basics, or to where you can enjoy programming.

In practical terms, I'll be glad to help you further, but only if you're willing to spend some serious effort yourself first. You'll find you get much better response and help, if you make sure you express yourself, your efforts, and your problem carefully and in detail. No need to call anyone sir either, we're all peers here.

Hope this helps,
 
1 members found this post helpful.
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc compiler cannot find header file stdio.h / stdlib.h debianlam Programming 2 05-11-2008 02:29 PM
can't find header file even tho I'm pointing directly to it kryptobs2000 Linux - Software 4 02-27-2008 08:41 AM
two dimensional array sorting in C/C++ George2 Programming 10 11-19-2006 08:29 PM
Perl Array Sorting craig467 Programming 8 10-02-2006 05:32 AM
I need help sorting an array in PHP! socceroos Programming 14 05-09-2006 02:37 AM


All times are GMT -5. The time now is 06:07 PM.

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