Skip to content

Instantly share code, notes, and snippets.

@motiejus
Created September 19, 2013 10:55
Show Gist options
  • Save motiejus/6621835 to your computer and use it in GitHub Desktop.
Save motiejus/6621835 to your computer and use it in GitHub Desktop.
/*
* Byte calculator. Sums bytes of a given file and displays to screen.
*
* Compile:
* gcc calc.c -o calc -O2
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define BUFFER (1 << 20)
unsigned long long do_calc(FILE *f) {
uint8_t buf[BUFFER];
unsigned long long sum = 0;
size_t nbytes, i;
while ((nbytes = fread(buf, 1, BUFFER, f)) > 0)
for (i = 0; i < nbytes; i++)
sum += buf[i];
return sum;
}
int main(int argc, const char* argv[]) {
FILE *f;
unsigned long long res;
if (argc != 2)
return 1;
if ((f = fopen(argv[1], "rb")) == NULL) {
perror("fopen");
return 1;
};
res = do_calc(f);
fclose(f);
printf("%llu\n", res);
return 0;
}
@yfyf
Copy link

yfyf commented Sep 20, 2013

Ok, so a 20 minute hack gave this (ccacl is the C code, hcalc is the Haskell code)

[~/sandbox/calc:master]λ sudo su -c 'sync && echo 3 > /proc/sys/vm/drop_caches'
[~/sandbox/calc:master]λ du -h big_file 
4.4G    big_file
[~/sandbox/calc:master]λ sudo su -c 'sync && echo 3 > /proc/sys/vm/drop_caches'
[~/sandbox/calc:master]λ time ./ccalc big_file 
599364006345

real    0m14.595s
user    0m2.670s
sys 0m2.450s
[~/sandbox/calc:master]λ sudo su -c 'sync && echo 3 > /proc/sys/vm/drop_caches'
[~/sandbox/calc:master]λ time ./hcalc big_file 
599364006345

real    1m17.030s
user    1m14.010s
sys 0m2.943s

The implementation is almost naive, except for one use of BangPatterns, but I think I can get rid of it.

I will optimise and share the code, but in any case, at least it works fine.

@yfyf
Copy link

yfyf commented Sep 20, 2013

Best I can do for now: https://gist.github.com/yfyf/6639162

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment