home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!stanford.edu!nntp.Stanford.EDU!news
- From: peer@ccrma.stanford.edu (Peer Landa)
- Subject: Code-optimizing . .
- Message-ID: <1993Jan7.000332.16632@leland.Stanford.EDU>
- Sender: news@leland.Stanford.EDU (Mr News)
- Organization: DSO, Stanford University
- Date: Thu, 7 Jan 93 00:03:32 GMT
- Lines: 72
-
-
- Is there someone out there who might have any idea why Example 1. runs faster
- then Example 2.? Is there a better way to optimize I/O then this
- "PUTSHORT"-macro and those loop's??
- I'd also like to know the reason why the following PUTSHORT macro:
- #define PUTSHORT(val, stream) if (1){ \
- register int v = val; \
- putc((v >> 8) & 0xff, stream); \
- putc(v & 0xff, stream); \
- } else
-
- .makes out-write faster then just doing a normal:
- outbuf = malloc(nsamples * sizeof(short));
- for (out = outbuf, in = inbuf, sEnd = in + nsamples; in < sEnd; ){
- *out++ = *in++ * 32767 / max;
- }
- ___________________________________________________
- ___ Example 1. ____________________________________
- FILE *out_file;
- register short *inbuf;
- int i, max, offset, in_file, nsamples, buffer_size;
- register int samp, j;
- register float x;
-
- max = i = 0;
- while (i < buffer_size) {
- nsamples = read(in_file, inbuf, buffer_size) / sizeof(short);
- /* finding the max value */
- for (j = 0; j < nsamples; j++) {
- samp = abs(*(inbuf+j)); /*negative to positive*/
- if (samp > max) {
- max = samp;
- }
- i += nsamples * sizeof(short);
- }
- x = 32767 / max;
- inbuf = &inbuf[0];
- for(j = 0; j < nsamples; j++) {
- PUTSHORT(*(inbuf+j) * x, out_file);
- }
- ___________________________________________________
- ___ Example 2. ____________________________________
- FILE *out_file;
- register short *inbuf, *in, *sEnd;
- int n_samples, in_file, nsamples, buffer_size, max;
- register int i, j, max_pos, max_neg;
-
- i = 0;
- while (i < buffer_size) {
- nsamples = read(in_file, inbuf, buffer_size) / sizeof(short);
- /* finding the max value */
- max_pos = max_neg = 0;
- for (in = inbuf, sEnd = in + nsamples; in < sEnd; in++) {
- if (*in < max_neg || *in > max_pos) {
- max_pos = abs(*in);
- max_neg = -max_pos;
- }
- }
- max = max_pos;
- i += nsamples * sizeof(short);
- }
- i = 32767 / max;
- for(j = 0, in = inbuf; j < nsamples; j++) {
- PUTSHORT((*in++) * i, out_file);
- }
- ___________________________________________________
-
- -- peer
-
-
-
-
-