"repeat" -- a C program written by KimiAI
Thursday, 5 February 2026 09:56 amI asked KimiAI if it could write a C program to repeat a string n times. I am a crappy C programmer. (I need to fix that shortcoming.) This program is optimised for just this task and beats all the hacks from my previous post, by a big margin.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <string> <count>\n", argv[0]);
fprintf(stderr, "Example: %s \"hello world\" 5\n", argv[0]);
return 1;
}
const char *str = argv[1];
int n = atoi(argv[2]);
size_t len = strlen(str);
if (n <= 0) return 0;
// Use write() directly with a large buffer
size_t unit = len + 1;
size_t total = unit * n;
char *buf = malloc(total);
for (int i = 0; i < n; i++) {
memcpy(buf + i*unit, str, len);
buf[i*unit + len] = (i == n-1) ? '\n' : ' ';
}
// Single syscall, no stdio overhead
write(STDOUT_FILENO, buf, total);
free(buf);
return 0;
}And compile with optimisations:gcc -O3 -o repeat repeat.c
no subject
Date: 2026-02-09 04:49 am (UTC)In high school I was very interested in how machine logic worked -- AND/OR/XOR/NAND/NOR/NXOR -- and using them to build adders and other things.
My very first computer was a Texas Instruments TI-57 programmable calculator, which fascinated me, and I wrote many programs for its very limited 50-step program memory. To this day I keep a TI-57/TI-59 emulator on my smartphone, which gets used surprisingly often.
The first "high-level" computer language I learned was BASIC for the Sinclair ZX-80, and its Z80 machine/assembly code painfully poked into memory as numbers.
Later I moved to a more complete BASIC for the TRS-80 and a proper assembler for its Z80.
Next was the Tandy Color Computer, affectionately known as the CoCo, with its greatly extended form of BASIC, and OS9 (very similar to Unix). I fell in love with its 6809 microprocessor -- writing assembly for it is like programming in a high level language. It made graphics programming easy, and its dual stack registers made it remarkably well suited to the most beautiful ugly language I've ever used: FORTH.
Later I got one of the first Amiga computers in Australia, and graphics programming exploded for me. Its 68000 processor had some similarity with the 6809, but I never really got into assembly for it. The enormous, rapidly growing, enthusiastic Amiga community shared a bewildering number of computer languages. I learned many, but became expert at none, until I became seduced by POV-Ray (originally DKBTrace, created by David K. Buck). POV-Ray taught me enormous amounts about rendering beautiful, 3D, photographic images.
And then I encountered VRML, which let me render 3D worlds in realtime and I gulped it down, to become one of the world experts in it... then it effectively died. I still make VRML worlds from time to time, but only rarely.
For some years I had a MicrosoftWindows computer (for paying work)... then I found Linux.
I ditched MSWindows for Linux and found software freedom similar to what I had during the Amiga years. Linux brought me even more languages to learn (but not master).
Today most of my programming is the bash shell and awk and sed.
Awk is a wonderfully fast and powerful interpreted language that comes with all Linux and Unix computers. It is small, so there is not too much to learn, and it is incredibly versatile, even though it is primarily a text-processing language. My only beef with it is that it doesn't do graphics, but I've found a way around that. :) There is an old set of image formats collectively called netpbm that can be expressed as ordinary text. The images are normal pixel images, but the files are straight text, making it perfect for manipulation by awk.
Sed is a weird, ugly little stream editor included with every Linux and Unix. Its language is very cryptic and compact, but blindingly fast and amazingly capable. It is not a general-purpose language (even though it is Turing-complete), but is great for modifying text. I use it constantly.
So that was an absurdly long-winded answer to a simple question. Sorry about that. :)
no subject
Date: 2026-02-13 09:12 am (UTC)My first experience was a TRS-80, then the Atari 2600 (it had a programming cart), and then I started learning BASIC proper with the VZ200 and did stuff like trying to code text adventures using the principle of Choose Your Own Adventure Books, nothing as complicated as "Get Lamp"), but I never had that kind of logical bent.
I learnt enough to read *some* machine and hex code (confidently badly and half-heartedly), and was good enough to troubleshoot most DOS/Windows issues and repair basic hardware/build machines if pressed, and even figure out how Apple worked, but I never cared enough to be that techly.
I agree with Linux. Win11 is horrific at all levels.
(Somehow, they managed to break Notepad, so it can run malicious code. NOTEPAD!)
no subject
Date: 2026-02-13 10:20 am (UTC)I always wanted to make a text adventure. The problem is, of course, that you have to write about ten times as much as would be needed in an ordinary short story -- most of it will never get read. Though I'm kinda used to that. Nobody reads most of my stories anyway. :)
Microsoft managed to mess up Notepad??? OMG!