miriam_e: from my drawing MoonGirl (Default)
[personal profile] miriam_e
I 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

Date: 2026-02-07 01:21 pm (UTC)
greylock: (Default)
From: [personal profile] greylock
So, you are older than me (I think), and I only know Basic, but you've kept up with machine/assembly code, and C?

AI or not, I'm impressed.

Date: 2026-02-13 09:12 am (UTC)
greylock: (Default)
From: [personal profile] greylock
Honestly. it's good for the memory to recount stuff like that. I'm younger than you and I am constantly forgetting things.

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!)

Profile

miriam_e: from my drawing MoonGirl (Default)
miriam_e

February 2026

S M T W T F S
123 4 567
8910 11121314
15161718192021
222324 25262728

Style Credit

Expand Cut Tags

No cut tags
Page generated Wednesday, 25 February 2026 09:22 pm
Powered by Dreamwidth Studios