|
It took some time to figure out that one... It is not memory access, which has uniform speed. This is index arithmetic, when "p[i]" is converted to address. When index crosses memory segments, it takes longer for index arithmetic to verify that segment being addressed is valid. If you replace p[i] = 'x'; with *p++ = 'x'; you will see that it takes slightly longer for first 16 meg, but it will be uniform all over. Above only applies to SLS pointers. If you compile the original program to use TS pointers CRTCMOD STGMDL(*TERASPACE) DTAMDL(*LLP64) it is also uniform all over, because index arithmetic no longer cares which segment you are addressing. Alexei Pytel always speaking for myself "Mike Mills" <mikem@xxxxxxxx> Sent by: To c400-l-bounces@mi "'C programming iSeries / AS400'" drange.com <c400-l@xxxxxxxxxxxx> cc 07/01/2004 03:01 Subject PM RE: [C400-L] RE: Porting C Linux to ILE C: Performance problem Please respond to C programming iSeries / AS400 Just following up my other message with some code you can use to measure memory access performance. It takes one required parameter - the amount of memory to manipulate, and one optional parameter - a flag indicating whether to print out the 'touch rate' for every megabyte (by default this is 'on'; specify '0' as the second parm to turn it off.) Anyway, the program measures three things: 1. How long it takes to perform the malloc(). 2. The 'touch rate' per million bytes (optional; on by default). 3. The total time it takes to touch all allocated bytes. The program uses 'gettimeofday()' in order to get microsecond resolution. Summarizing the results I get on my V5R2 machine, for 24,000,000 bytes: malloc() time: 27,392 microseconds Typical time to touch 1 million bytes BELOW 16MB: around 225,000 microseconds Typical time to touch 1 million bytes ABOVE 16MB: around 3 seconds + 300,000 microseconds By the way, this affects Java applications as well (try to use byte[] arrays >16MB.) Following is the code. Note that you have to specify the 'teraspace' options on the C compiler in order to allocate >16MB of contiguous memory. /*** BEGIN PROGRAM ***/ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> void computeTimeDiff(struct timeval *start, struct timeval *finish, struct timeval *difference) { difference->tv_sec = finish->tv_sec - start->tv_sec; difference->tv_usec = finish->tv_usec - start->tv_usec; while(difference->tv_usec < 0) { difference->tv_sec--; difference->tv_usec += 1000000L; } return; } int main(int argc, char *argv[]) { int mem = 0, i = 0, showRate = 1; char *p = NULL; struct timeval beforeMalloc, afterMalloc, beforeLoop, afterLoop; struct timeval mallocDiff, loopDiff; struct timeval mbStart, mbCurrent, mbDiff; if(argc < 2) { printf("Usage: %s <memory size>\n", argv[0]); exit(1); } if(argc > 2) /* Check 'showRate' flag */ { if((argv[2][0] == 'n') || (argv[2][0] == 'N') || (argv[2][0] == '0')) { showRate = 0; } } mem = atoi(argv[1]); if(mem < 1) { printf("%s: must specify memory size > 0\n", argv[0]); exit(1); } gettimeofday(&beforeMalloc, NULL); p = malloc(mem); gettimeofday(&afterMalloc, NULL); computeTimeDiff(&beforeMalloc, &afterMalloc, &mallocDiff); printf("Attempt to malloc '%d' bytes took: %ld sec and %ld us\n", mem, mallocDiff.tv_sec, mallocDiff.tv_usec); if(p == NULL) { printf("%s: failed to malloc '%d' byts of memory\n", argv[0], mem); exit(1); } gettimeofday(&beforeLoop, NULL); gettimeofday(&mbStart, NULL); for(i = 0; i < mem; i++) { if(showRate && (i >= 1000000) && ((i % 1000000) == 0)) { gettimeofday(&mbCurrent, NULL); computeTimeDiff(&mbStart, &mbCurrent, &mbDiff); printf("Current offset: %d, rate: %ld s and %ld us\n", i, mbDiff.tv_sec, mbDiff.tv_usec); gettimeofday(&mbStart, NULL); } p[i] = 'x'; } gettimeofday(&afterLoop, NULL); computeTimeDiff(&beforeLoop, &afterLoop, &loopDiff); printf("Touching '%d' bytes took: %ld sec and %ld us\n", mem, loopDiff.tv_sec, loopDiff.tv_usec); printf("\nFinished!\n\n"); return(0); } /*** END PROGRAM ***/ -- Mike Mills mikem@xxxxxxxx ConsulTech Information Systems, Inc. _______________________________________________ This is the C programming iSeries / AS400 (C400-L) mailing list To post a message email: C400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options, visit: http://lists.midrange.com/mailman/listinfo/c400-l or email: C400-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at http://archive.midrange.com/c400-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.