C language qsort function algorithm performance test

Source: Internet
Author: User

C language qsort function algorithm performance test

An intuitive method to measure the complexity of an algorithm is to measure the running time of an algorithm of a certain magnitude of data.

Taking the qsort provided by C language as an example, we can test the computing time of the qsort with 1 million of the data volume to detect the time cost of O (nlg (n:

The C code is as follows:

# Include

 
  
# Include

  
   
# Include

   
    
# 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 <10; 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 <10; I ++) printf ("% f", x [I]); printf ("\ n"); clock_t t2 = clock (); printf ("when the floating point sorting algorithm is used: % f second \ n ", (double) (t2-t1)/CLOCKS_PER_SEC); return 0 ;}

   

  

 

Compile and run the SDK with gcc qsort_test.c in The Notebook. 1 million data is randomly generated and sorted 10 times. The result is as follows:

~ /Tmp $./a. out
0.000000 0.000001 0.000001 0.000002 0.000002 0.000004 0.000004 0.000005 0.000006 0.000006
Floating Point Number Sorting Algorithm time: 2.236941 seconds

Related Article

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.