Skip to content

Instantly share code, notes, and snippets.

@KristianLyng
Created November 3, 2011 09:11
Show Gist options
  • Select an option

  • Save KristianLyng/1336100 to your computer and use it in GitHub Desktop.

Select an option

Save KristianLyng/1336100 to your computer and use it in GitHub Desktop.
root@luke:~/tmpfs# cat foo.sh
rfile=$(mktemp)
size=$1
gcc -x c -O3 -o $rfile - <<__EOF__
#include <stdlib.h>
int main(void) {
int i;
long int a;
char *b;
char buff[100010];
int buflen=0;
int intcounter;
srandom(time(NULL));
b = (char *) &a;
for (i=0;i < ($1 / sizeof(long int));i++) {
a = random();
for (intcounter=0; intcounter<sizeof(long int); intcounter++)
buff[buflen+intcounter] = b[intcounter];
buflen+=intcounter;
if (buflen > 100000){
write(1,buff,buflen);
buflen=0;
}
}
write(1,buff,buflen);
return 0;
}
__EOF__
$rfile > tmpfile; ls -lh tmpfile
rm $rfile
root@luke:~/tmpfs# time ./foo.sh 40000000
-rw-r--r-- 1 root root 39M Nov 3 10:10 tmpfile
real 0m0.144s
user 0m0.116s
sys 0m0.032s
root@luke:~/tmpfs#
@KristianLyng
Copy link
Author

Or for something bigger?

root@luke:~/tmpfs# time ./foo.sh 4000000000
-rw-r--r-- 1 root root 3.8G Nov 3 10:13 tmpfile

real 0m8.097s
user 0m6.828s
sys 0m1.280s

@cd34
Copy link

cd34 commented Nov 3, 2011

Why not just do:

dd if=/dev/random of=tmpfile bs=1000 count=4000

or /dev/urandom if you're on a machine without a lot of entropy.

I do like the evilness, but, seems like a lot of overkill. :)

@KristianLyng
Copy link
Author

Because urandom is ridiculously slow. On my rather fast system /dev/urandom (Which is the FAST one) pulls 7.5MB/s.

@KristianLyng
Copy link
Author

Oh, it's also obviously mostly a joke now. A far better solution is just using shred, which gives comparable speeds. But I still like the vileness :P

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