Efficiency Test and optimization of C/C ++ For loop statements and runtime errors: Stack Overflow Solution

Source: Internet
Author: User

C/C ++ For loop statement efficiency test optimization and runtime error: Stack Overflow solution in multiple loops, if possible, should put the longest loop in the innermost layer, the shortest cycle is placed on the outermost layer to reduce the number of times the CPU switches across the cycle layer. Use the following code to test the efficiency of nested For loops: [cpp] // For nested performance. cpp: Defines the entry point for the console application. // # include "stdafx. h "# include <iostream> # include <time. h> using namespace std; int main (void) {clock_t cstart, cends; cstart = clock (); long sum1 = 0; long a1 [5] [1, 10000000]; for (long I = 0; I <5; I ++) {for (long j = 0; j <10000000; j ++) {sum1 + = a1 [I] [j] ;}} cout <"sum1 =:" <sum1 <endl; cends = clock (); cout <"Clock time difference:" <cends-cstart <endl; //////////////////////////////////////// /// // cstart = clock (); long sum = 0; long a [10000000] [5]; for (I = 0; I <10000000; I ++) {for (long j = 0; j <5; j ++) {sum + = a [I] [j] ;}} cout <"sum =:" <sum <endl; cends = clock (); cout <"Clock time difference:" <cends-cstart <endl; //////////////////////////////////////// /// // system ("pause "); return 0;} running result: the above Code is compiled on Windows Server 2008x64. The IDE is Microsoft Visual c ++ sp6 and can be compiled, but an error occurs during running: runtime error: Stack Overflow solution:


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.