Code snippet below, Linux, malloc200W and 2 w, takes about 15 times the gap, is the reason why and how to optimize excuse me?
Char * * cc;
Cc=malloc (sizeof (char *) * 2000000);
The gettimeofday (& amp; Tv1, 0);
Char CCC='a';
* (cc + 0)='a';
The gettimeofday (& amp; Tv2, 0);
Fprintf (stdout, "% % 2. % lu, lu, lu, lu % \ n", tv1. Tv_sec, tv1. Tv_usec, tv2. Tv_sec, tv2. Tv_usec);
CodePudding user response:
Supplement, take on assignment, not malloc, malloc gap can be ignored,
CodePudding user response:
This assignment is not all the same?
CodePudding user response:
Assignment is the same, the key is the array size is not the same, a 20 w, 2 w a, whether the size is different in different addressing process
CodePudding user response:
Not discriminating, you posted the testing result
CodePudding user response:
Sorry, just described is a bit wrong, assignment is 200 w on different position, time consuming, below is the code and the results
Char * * cc;
Cc=malloc (sizeof (char *) * 2000000);
The gettimeofday (& amp; Tv1, 0);
* (cc + 0)='a';
The gettimeofday (& amp; Tv2, 0);
Fprintf (stdout, "% % 2. % lu, lu, lu, lu % \ n", tv1. Tv_sec, tv1. Tv_usec, tv2. Tv_sec, tv2. Tv_usec);
The gettimeofday (& amp; Tv1, 0);
* (cc + 1500000)='a';
The gettimeofday (& amp; Tv2, 0);
Fprintf (stdout, "% % 2. % lu, lu, lu, lu % \ n", tv1. Tv_sec, tv1. Tv_usec, tv2. Tv_sec, tv2. Tv_usec);
The test results
2.1589117027, 554010158117, 027554, 014
2.1589117027, 554016158117, 027554, 282
CodePudding user response: