Joseph K. Myers

Monday, May 13, 2002

C, Character Repeats

A singularly confusing dilemma is placed before a computer programmer, that of solving a problem which is itself the simplest, most ordinary, and therefore the most mesmerizing affront to an intelligent human being.

How do you create the string '!!!'--or, for that matter, the string '-------'? What about ten million copies of the letter A?

Nothing could be more simple or more complicated--simple because it could be done any way, and complicated because the fastest ways are inanely elusive.

The c program performs rapid generation of n occurrences of character c. With n < 0, no limit is reached. The value of c defaults to - (ASCII 45).

Other cool things:

Just for fun I have included a file (156 bytes) consisting of the triple-gzipped output of c 8388608, rr'ed 128 times to form the equivalent of c 1073741824 (that is, one gigabyte of hyphens). Compression strengths were 7, 7, and 9 (gzip -Ncnv) with input-output ratios of 99.9, 99.8, and 90.1% percent. The overall reduction factor (bytes in / bytes out) is ~ 6,882,960. The output is 1.45e-7 times the size of the input.

Compare _that_ to 10/1, 100/1, or even 10,000/1 compression. ;-) (Well, duh!)

Usage:

c [n] [c]

Examples:

`c` = -...-;

`c 5` = -----;

`c 1 a` = a;

Download:

All files are in c.tar.gz (754 bytes).

Installation:

A Makefile is provided. Install c into /usr/local/bin. (Something like "sudo cp c /usr/local/bin" ought to work.)

Limitations:

The character count (n) is limited by the size of a "long int" as defined on your machine.

Performance seems to be about 5,000,000,000 characters/sec on a 333 MHz test computer. The first 0..4096 characters are produced slightly slower. One test on the same computer returned 10,000,000,000 bytes in a time of 0.350 sec.

And hey you know, the server *did* manage 125,000,000,000 bytes (125 GB) per second!

Oh by the way, those millions of A's (ten, to be exact) can be produced in <= 0.0004 sec. (I can't tell since the timer is only accurate to the thousandths.)

http://www.myersdaily.org/joseph/unix/c.txt