Writing one line at a time means one system call per line.
Unbuffered I/O turns every small read or write into its own trip to the operating system and the disk. Log files, CSV exports and socket writes are the usual culprits.
GreenCode researchers list unbuffered reads and writes among the worst energy wasters in code.
GreenCode helps tackle this by rewriting flagged code one piece at a time. It writes any missing tests first, and returns every change for a person to approve. More on this.
Writing one line at a time means one system call per line. Each call is a separate trip to the operating system, and often the disk. Log files, spreadsheet exports and network writes are the usual culprits. A million lines can mean a million trips. Every trip costs processor time, and that means electricity. Here is the fix you can use today. Wrap your streams in a buffered reader or writer. Then thousands of small calls become a handful of big ones. Write in chunks, and stream large files instead of loading them whole. Flush when you need the data to be safe, not after every line. GreenCode's Constitution skill can help an AI coding agent find and fix this. Then check the saving, using tools like green-languages to measure before and after. GreenCode researchers list unbuffered reads and writes among the worst energy wasters in code. GreenCode helps tackle this by rewriting flagged code one piece at a time. It writes missing tests first, and a person approves every change. That was GreenCode Tech Tip number twenty-two. Follow for the next tip, and find more on greencode.ai/tech-tips.
Share this