What the fuck is wrong with python?
Test file: 33752686 lines of 10 random characters, 354M
Test one:with open('out') as F:
strs = F.readlines()
newlist = []
for i in strs:
newlist.append(i.upper())
real 0m29.758s
user 0m17.106s
sys 0m9.439s
Test two:with open('out') as F:
strs = F.readlines()
upper = str.upper
newlist = []
append = newlist.append
for i in strs:
append(upper(i))
real 0m19.691s
user 0m16.086s
sys 0m3.348s
?????????
>>57863213
I am in halfway long project (6000+lines) using Python/Django and I am cursing this shit all day,
I wish I used Go or Elixir
>>57863213
In the second one:
>upper = str.upper
You didnt even define str. Or is str a standard object? I do not know python
>>57863233
>elixir
>read file
well.
>>57863213
what the fuck is the point of this benchmark
>>57863454
That type function lookups are slow due to the lack of a JIT.
hey, try this:with open('out') as f:
newlist = list(map(str.upper, f.readlines()))
>>57863276
Yes senpai
I don't even know code
>>57863620
The python way.
>>57863477
There's pypy
>>57863620real 0m15.967s
user 0m11.961s
sys 0m3.730s
Noice
>>57863746
Not OP, thanks for this, havent heard of this before.
>ruby3: let's make it 3 times faster!
>python3: let's make all the code broken!
>>57863213
It has to do lookups every time because it's just so damn dynamic. str.upper could be a @property that returns a new object every time. There's no way for it to know so it can't cache the lookup. Python is meant to be easy and quick to write glue code. If this is your bottleneck then you are doing it wrong.
>>57864039
Calm down mr Autism, that's exactly what I was trying to show in my example.
If you're doing the same task on every line anyway, then you can just dowith open('out') as F:
whatever = str.upper(F.read())
real 0m1.109s
user 0m0.483s
sys 0m0.604s
>>57863213
Would you have the file perhaps so I can benchmark too?
Files are iterators now:tnewlist = []
with open('out') as f:
for line in f:
newlist.append(f.read().upper())
>>57864181
Just generate a file, there is nothing specificdef junk():
abc = 'abcdefghijklmnopqrstuvwxyz01234567890'
with open('out', 'w') as F:
for i in range(8500000):
F.write("".join(sample(abc, 10)) + "\n")
And fuck anon that's fast:newlist = []
with open('out') as F:
newlist.append(F.read().upper())
real 0m1.108s
user 0m0.485s
sys 0m0.613s
>>57863620
>with as f:
What is that syntax for? loop?
>>57864254
"Open this file, refer to the file object as f and close it when done"
>>57864209
Temporal and spatial locality
>>57864254
poorman version ofFile.open('out') do |f|
end
>>57864254
>>57864285
its actually a python class with an __open__ and __exit__ function. Open calls the __open__ function within and exit is called when you exit the loop.
The idea is that you can create your own classes so that exit() gracefully handles exceptions and cleans up your environment when done. In open's case it opens the file and gracefully closes it without needing F.close()
>>57863213
if you want fast code, don't write in python. end of story.
>>57864209
Weird, doesn't really seem to work for me (800K lines, atom shit cpu)Function <function as_iterator>ran in: 9.590599298477173s
Function <function using_map>ran in: 10.747447967529297s
Function <function unoptimized>ran in: 10.441354751586914s
>>57864254
Similar to using in C# and try with ressources in java
>>57864328
or spend the time to actually optimize your code using cython