Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm doing the challenges in PowerShell to see how it goes.

I want to use it as a test to see how human programming can be improved by an AI, so I wrote the solution for day 1, got the right answer, and then gave my code to ChatGPT 4o to ask it to make the code faster.

My version ran in ~3500 ms ChatGPT's version ran in 140 ms

both worked

A great example of how a common DevOps language program can be improved on by ChatGPT.



That seems rather slow for yours, and not very fast for an optimised one. It can speed up a lot from a cold start to a warm run, my tuned code can show 8 ms in powershell 7.4 after a few runs.

My hack-it-out code: https://pastebin.com/PDQhxDc9

Faster code: https://pastebin.com/6xwaVkwq

The hacky code uses slower techniques like:

- Get-Content which adds metadata to every line.

- @() arrays with += which copies the array in memory to a new one-larger memory location for every addition.

- Pipeline overhead e.g. ForEach-Object and Measure-Object.

- Filtering the whole second column for each number in the first column, repeated wasted work.

and it's still in the region of your ChatGPT one.

The faster one addresses these with:

- ReadAllLines() .NET method to get plain strings.

- [system.collections.generic.list[int]]::new() which don't box integers and can grow more quickly.

- plain adding numbers into sum variables.

- Building a hashtable [system.collections.generic.dictionary[int, int]]::new() to count each number in the second column.

- Swapping -split for string split() which may have a tiny bit less overhead.

- no pipelines.

The code isn't completely different, it's the same blocks doing the same things, leaning more on .NET lower levels, and years of experience of the basic PowerShell performance hits.


Great to see your optimizations.

I'm going through each day and asking ChatGPT to speed it up, it doesn't always work, but that is the way of the Gippity.

The payoff is these heavy iterating operational scripts I run for work will end up running faster :)

Learning when to lean on some of the .NET primitives really helps speed up PS as it likes to wrap things with unnecessary features.


You may find Claude giving better results. You can ask her to write code with "please apply technical guidance from Stephen Toub w.r.t. performance to the code you come up with" and it helps to improve the quality since Stephen Toub is someone she knows about. Or maybe some other core contributors and article authors that have more likelihood to be present in the dataset.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: