Home > Back-end >  Could you tell me an array subscript efficiency problem
Could you tell me an array subscript efficiency problem

Time:09-23

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:

reference 5 floor alldying reply:
I'm 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

This test is not very reasonable, you * (cc + 0)='a'; The compiler is not that stupid, will be optimized, so you should change position don't use offset 0,
Second your variables for char type, then the offset addressing, address operation is much for addressing take the number of assembly code addressing the fastest and the bus is the same digit
Another test efficiency, allocate memory you should try to allocate in a physical memory page,

CodePudding user response:

Thank gouyanfen reply,


1, 10 to migration, the results about the same, this test is to want to know about a large array of different positions, different from its starting point is the distance from the location of the assignment if they take consistent;

Char * * cc;
Cc=malloc (sizeof (char *) * 2000000);
The gettimeofday (& amp; Tv1, 0);
* (cc + 10)='a';
The gettimeofday (& amp; Tv2, 0);
Fprintf (stdout, "2.1. % 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.2. % lu, lu, % % lu, lu % \ n", tv1. Tv_sec, tv1. Tv_usec, tv2. Tv_sec, tv2. Tv_usec);

The test results

2.1. 1589123428159 323158123 428159 328
2.2. 1589123428159 329158123 428159 613

2, the variable is the char type is because there is a custom structure in practical project, the structure size can't keep like bus digits;

3, although the purpose of this test is not memory allocation efficiency, however, how to allocate in a physical memory page, in that case the assignment should also be high efficiency;

CodePudding user response:

The
reference 7 floor alldying response:

I think you can put the cc to unsigned int + offset before operation, do not use his own type
* (cc + 150000)='a'
This operation is cc + sizeof (char) * 150000
* (char *) ((int) cc + 150000)='a';
It will be faster

CodePudding user response:

I take the Linux to explain, is not entirely accurate, can approximate understand it,

When you malloc 200 w bytes system actually give you a blank check, although you got the address of the allocated memory, but no allocation at all in the underlying physical memory, just made a record, when you start to write something in memory, the system to assign you a real physical memory, but also easy to imagine that you write the address of the offset, the greater the system needs to be really ready to physical memory, the more the longer the time,

Want to accurately explain, can go to the virtual memory on this,

CodePudding user response:

Modified into
* (char *) ((int *) cc + 150000)='a';
After run 2 times, have significantly improved for the first time, the second result as before, as a result, the following

2.1. 1589128544452 039158128 544452 045
2.2. 1589128544452 047158128 544453 108


2.1. 1589128846724 661158128 846724 667
2.2. 1589128846724 669158128 846724 929

Later to run multiple times, the result is similar to the second,

Speculation, for the first time just as you said above, the distribution of cc in the same physical pages, so a lot of fast, because single from an array, the complexity of the operation position should be O (1), but by addressing this step, if across the page, then the farther from the starting point is the more steps, the more time consuming,
  • Related