diff options
author | Jiří Pinkava <j-pi@seznam.cz> | 2014-10-03 14:56:52 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2015-08-21 01:49:38 +0200 |
commit | c065f9214c758ead2c746b5ef7d57837bcc6edd4 (patch) | |
tree | a502bfe36bc83a0334fa88c86b5414fc238bad5b | |
parent | 6f78d76052034e655b6c5672fe7ed0ab27e24c39 (diff) |
speed up float_to_bits
Reduce overhead caused by inefficient call of read
for each float. Increases speed about 50x.
-rw-r--r-- | src/float_to_bits.c | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/float_to_bits.c b/src/float_to_bits.c index 3fed32b..9ececa0 100644 --- a/src/float_to_bits.c +++ b/src/float_to_bits.c @@ -71,6 +71,9 @@ static void sym_int2bits(int sym, uint8_t *ret) } } +// size of IO buffers (number of elements) +#define BUF_SIZE 1024 + int main(int argc, char **argv) { int fd, fd_out, opt; @@ -104,21 +107,27 @@ int main(int argc, char **argv) } while (1) { int rc; - float fl; - uint8_t bits[2]; - rc = read(fd, &fl, sizeof(fl)); + float fl[BUF_SIZE]; + uint8_t bits[2*BUF_SIZE]; + rc = read(fd, fl, sizeof(*fl) * BUF_SIZE); if (rc < 0) { perror("read"); exit(1); - } else if (rc == 0) + } else if (rc == 0) { break; - rc = process_sym_fl(fl); - sym_int2bits(rc, bits); - //printf("%2d %1u %1u %f\n", rc, bits[0], bits[1], fl); - if (opt_verbose) - printf("%1u%1u", bits[0], bits[1]); + } + rc /= sizeof(*fl); + int i; + for (i = 0; i < rc; ++i) { + int sym = process_sym_fl(fl[i]); + sym_int2bits(sym, bits + i*2); + //printf("%2d %1u %1u %f\n", rc, bits[0], bits[1], fl); + if (opt_verbose) { + printf("%1u%1u", bits[2*i + 0], bits[2*i + 1]); + } + } - rc = write(fd_out, bits, 2); + rc = write(fd_out, bits, rc * 2); if (rc < 0) { perror("write"); exit(1); |