Been trying some dumb benchmarks, which I'm sure are really flawed, but which I think may be a good learning opportunity on how Swift works under the hood:
As you can see Swift seems to be slower than Python in this particular benchmark. Swift is supposed to be faster and has the advantage of not having to dynamically resize the array in this case. So I am left wondering if anyone knows whats happening?
PD: If I make the array to fill 100 times larger, then Swift becomes 10 times faster than Python which is more similar to what I was expecting. Also, I created a similar reddit thread, though I think the benchmark in that post is worse as it has more moving parts.
Hmm. Your benchmark here is is showing ~0.3 milliseconds to complete for both languages. Putting each of these iterations within another layer of iterations to force the test of 3000 appends to run for longer may be a better way to measure this. The difference here is so small that you may just be hitting the runtime startup/shutdown so it's not really measuring the append performance.
Also, it might help if you posted the times you get for both when making the array 100 times larger.
Can the optimizer see through the loop and simply perform a memset? If not, perhaps a bug report should be filed for a missed optimization opportunity.
I am guessing that initial cost is the runtime startup you were mentioning. I am not exactly sure what that means though, is it like a set of instructions that swift uses to do the bare minimum that have to be loaded into ram? Why is it doing this loading in the middle of the program instead of before running any of the instructions I wrote? Or maybe if you have any links or something I could read on this I could take less of your time. Thanks a lot!
Oh, and regarding your second quesion, when making it 100 times larger the times are:
Swift: 0.00032
Python: 0.036
There is some amount of unavoidable startup cost that pretty much every process pays for, to do dynamic linking and C runtime startup, even before main is entered, which would affect both Python and Swift. Both Python's and Swift's runtimes probably add a small amount of additional warmup cost; Swift tries to keep this minimal, though, and I imagine Python does as well, by bringing up runtime services lazily as needed by the app.
Just in case anyone else is interested, ran the same test but the other way around, comparing Swift to C. If I use reserveCapacity and use the Clang compiler with no flags Swift has an overhead of around 20% over C, which looks quite impressive imo.
If I turn on -O3 -march=native in Clang though, then C is around 10 times faster. Swift is allocating the array in the heap and C in the stack though, so its not super fair either I guess.