Skip to content

Instantly share code, notes, and snippets.

@hmkemppainen
Created December 15, 2019 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmkemppainen/376b973c568fc122e2d8c843b49e8d0d to your computer and use it in GitHub Desktop.
Save hmkemppainen/376b973c568fc122e2d8c843b49e8d0d to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void insb(uint8_t b, size_t off, uint8_t **buf, size_t *buflen)
{
assert(off <= *buflen);
uint8_t *nb = realloc(*buf, *buflen + 1);
if (!nb) err(1, "realloc");
memmove(nb + off + 1, nb + off, *buflen - off);
nb[off] = b;
(*buflen)++;
*buf = nb;
}
int main(int ac, char **av)
{
FILE *f = fopen("100M", "r");
uint8_t tmp[0x4000], *buf = NULL, *nb;
size_t buflen = 0;
ssize_t n;
if (!f) err(1, "fopen");
while ((n = fread(tmp, 1, sizeof tmp, f)) > 0) {
nb = realloc(buf, buflen + n);
if (!nb) err(1, "realloc");
memcpy((buf = nb) + buflen, tmp, n);
buflen += n;
}
if (ferror(f))
err(1, "fread");
fclose(f);
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
fprintf(stderr, "%ld.%03ld\n", ts.tv_sec, ts.tv_nsec);
insb('X', buflen / 2, &buf, &buflen);
clock_gettime(CLOCK_REALTIME, &ts);
fprintf(stderr, "%ld.%03ld\n", ts.tv_sec, ts.tv_nsec);
fwrite(buf, 1, buflen, stdout);
return 0;
}
/*
$ time ./a.out > 100M1
1576370676.960357900
1576370676.964517730
real 0m0.084s
user 0m0.006s
sys 0m0.078s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment