home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19340 < prev    next >
Encoding:
Text File  |  1993-01-06  |  2.3 KB  |  83 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!nntp.Stanford.EDU!news
  3. From: peer@ccrma.stanford.edu (Peer Landa)
  4. Subject: Code-optimizing . .
  5. Message-ID: <1993Jan7.000332.16632@leland.Stanford.EDU>
  6. Sender: news@leland.Stanford.EDU (Mr News)
  7. Organization: DSO, Stanford University
  8. Date: Thu, 7 Jan 93 00:03:32 GMT
  9. Lines: 72
  10.  
  11.  
  12. Is there someone out there who might have any idea why Example 1. runs faster  
  13. then Example 2.?  Is there a better way to optimize I/O then this  
  14. "PUTSHORT"-macro and those loop's??
  15. I'd also like to know the reason why the following PUTSHORT macro:
  16.     #define PUTSHORT(val, stream) if (1){ \
  17.     register int v = val; \
  18.     putc((v >> 8) & 0xff, stream); \
  19.     putc(v & 0xff, stream); \
  20.     } else
  21.  
  22. .makes out-write faster then just doing a normal:
  23. outbuf = malloc(nsamples * sizeof(short));
  24. for (out = outbuf, in = inbuf, sEnd = in + nsamples; in < sEnd; ){
  25.     *out++ = *in++ * 32767 / max;    
  26. }
  27. ___________________________________________________
  28. ___ Example 1. ____________________________________
  29. FILE    *out_file;
  30. register    short    *inbuf;
  31. int    i, max, offset, in_file, nsamples, buffer_size;
  32. register int    samp, j;
  33. register float    x;
  34.     
  35. max = i = 0;
  36. while (i <    buffer_size) {    
  37.     nsamples = read(in_file, inbuf, buffer_size) / sizeof(short);
  38.     /* finding the max value */
  39.     for (j = 0; j < nsamples; j++) {
  40.         samp = abs(*(inbuf+j)); /*negative to positive*/
  41.         if (samp > max) {
  42.             max = samp;
  43.         }
  44.         i += nsamples * sizeof(short);
  45.     }
  46. x = 32767 / max;
  47. inbuf = &inbuf[0];
  48. for(j = 0; j < nsamples; j++) {
  49.     PUTSHORT(*(inbuf+j) * x, out_file);
  50. }
  51. ___________________________________________________
  52. ___ Example 2. ____________________________________
  53. FILE    *out_file;
  54. register short    *inbuf, *in, *sEnd;
  55. int    n_samples, in_file, nsamples, buffer_size, max;
  56. register int     i, j, max_pos, max_neg;
  57.  
  58. i = 0;
  59. while (i <    buffer_size) {    
  60.     nsamples = read(in_file, inbuf, buffer_size) / sizeof(short);
  61.     /* finding the max value */
  62.     max_pos = max_neg = 0;
  63.     for (in = inbuf, sEnd = in + nsamples; in < sEnd; in++) {
  64.         if (*in < max_neg || *in > max_pos) {
  65.             max_pos = abs(*in);
  66.             max_neg = -max_pos;
  67.         }
  68.     }
  69.     max = max_pos;
  70.     i += nsamples * sizeof(short);
  71. }
  72. i = 32767 / max;    
  73. for(j = 0, in = inbuf; j < nsamples; j++) {
  74.     PUTSHORT((*in++) * i, out_file);
  75. }
  76. ___________________________________________________
  77.  
  78. -- peer
  79.  
  80.  
  81.  
  82.  
  83.