Performance test of C-language qsort function algorithm

Source: Internet
Author: User

For the complexity of the algorithm. A direct method is to measure the perception of the execution time of a quantitative algorithm-level data.

As the C language provides qsort for example. With 10.01 million times of data to test its computational capacity. Perceived O (NLG (n)) time cost:

C Code as below:

#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 1000000//int (*comp) (const void *, const void *) int compare (const void *P1, const void *P2) {return * (float*) p1 > * (float*) P2;} int main () {float X[n];srand (time (NULL)), clock_t T1 = Clock (), for (int j = 0; J <; J + +) {for (Long i = 0; i < N; I + +) X[i] = (float) rand ()/rand_max;qsort (x, N, sizeof (float), compare);}  for (int i = 0; i < i++) printf ("%f", X[i]);p rintf ("\ n"), clock_t t2 = Clock ();p rintf ("Time for floating-point sorting algorithm:%f sec \ n", (double) (T2 -T1)/clocks_per_sec); return 0;}

Compile and execute with GCC qsort_test.c on your notebook. 10 plays 1 million random data generation and sequencing results are:

~/tmp$./a.out
0.000000 0.000001 0.000001 0.000002 0.000002 0.000004 0.000004 0.000005 0.000006 0.000006
When sorting floating-point algorithm: 2.236941 second

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Performance test of C-language qsort function algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.