The speed at which STL learns to test stlen,cout, etc.

Source: Internet
Author: User

Early to hear that is C + + running speed than C is a lot slower, so write a small program to test, in the end how slow.

1#include <stdio.h>2#include <time.h>3#include <string.h>4 intMain ()5{clock_t a=clock ();6     inti,j,k;7     Charc[10000]={"ASSDASDXZCZASDXZCZZSDAAAASDXZCZZSDAAAZSDAAAAAAAAAAAAAASDASFSDXZCZZSDAAAAAAAAAAAAAASDASFZVFASDQWEASFASSDXZCZZSDAAAAAAA AAAAAAASDASFZVFASDQWEASFASSDXZCZZSDAAAAAAAAAAAAAASDASFZVFASDQWEASFASZVFASDQWEASFASF"};8     intL =strlen (c);9     Ten      for(i =0; I < strlen (c); i++) One          for(j =0; J < strlen (c); J + +) A              for(k =0; K < strlen (c); k++) -                  ; -  theclock_t b=clock (); -  -printf"%lf,%d",(Double) (B-A)/clk_tck,l); -}

Called time. H inside of the clock_t class, using the clock function to get two times the system always, because the number of shocks is obtained so also need to divide by clk-tck to calculate the time in seconds. The last is to compare the consumption time of the two functions, in order to make the effect obvious point, according to the use of 200-length character array, and then the triple loop, so that the number of strlen () function is 200 of 3 times. 8 million plays

Run a few times the time is probably 1s up and down, and then the program in the strlen replaced with a fixed variable l then will only run 8 million times alone,

Can be compared to the approximate time is very slow, probably a few times the slowest.

Then I want to test that the CIN and cout in C + + are slower than the scanf and printf comparisons in C.

#include <iostream>#include <cstdio>#include <ctime>#include <cstring>UsingNamespaceStdIntMain () {clock_t a=Clock ();IntI,j,k;int L =100;for (i =0; I < L; i++)for (j =0; J < L; J + +) cout<< "a" ;;; clock_t b=clock (); for (i = for (j = 0; J < L; J++) printf ( "a" );; clock_t c=clock (); printf ( " \n\n%lf\n%lf "( Double) (b-a)/clk_tck, (clk_tck);}    

Executed 10,000 times, the result is

It can be said that it is a little slower, but there are also unexpected problems, that is, if the use of this function causes the speed of the operation is slow, then if I run the number of times and time will be a certain proportion, then if I turn the number of runs to larger?

After I expanded 10 times times, the result was

Not fully proportional to the full scale.

The problem here is that the CPU may be in the operation of the time can be used in the internal direct register to reduce the number of reads and the speed of the method, in any case today can be counted to calculate the time of the two methods of the pros and cons of it.

The speed at which STL learns to test stlen,cout, etc.

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.