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
?????????
Christopher Brooks
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
Adrian Evans
In the second one:
>upper = str.upper
You didnt even define str. Or is str a standard object? I do not know python
Gavin Reed
>elixir >read file
well.
Easton Gray
what the fuck is the point of this benchmark
Lincoln White
That type function lookups are slow due to the lack of a JIT.
Charles Gray
hey, try this: with open('out') as f: newlist = list(map(str.upper, f.readlines()))
Jason Bell
Yes senpai
I don't even know code
Daniel Harris
The python way.
Jordan Jenkins
There's pypy
Ayden Howard
real 0m15.967s user 0m11.961s sys 0m3.730s
Noice
Samuel Bailey
Not OP, thanks for this, havent heard of this before.
Connor Jackson
>ruby3: let's make it 3 times faster! >python3: let's make all the code broken!
Cameron Rogers
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.
Anthony Wright
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 do with open('out') as F: whatever = str.upper(F.read())
real 0m1.109s user 0m0.483s sys 0m0.604s
Caleb Hughes
Would you have the file perhaps so I can benchmark too?
Files are iterators now:t newlist = [] with open('out') as f: for line in f: newlist.append(f.read().upper())
Ethan Bennett
Just generate a file, there is nothing specific
def junk(): abc = 'abcdefghijklmnopqrstuvwxyz01234567890' with open('out', 'w') as F: for i in range(8500000): F.write("".join(sample(abc, 10)) + "\n")
And fuck user that's fast: newlist = [] with open('out') as F: newlist.append(F.read().upper())
real 0m1.108s user 0m0.485s sys 0m0.613s
Christian Thompson
>with as f: What is that syntax for? loop?
Carter Rodriguez
"Open this file, refer to the file object as f and close it when done"
Jason Ward
Temporal and spatial locality
Anthony Thomas
poorman version of File.open('out') do |f| end
Blake Rivera
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()
Angel Phillips
if you want fast code, don't write in python. end of story.
Kevin Cooper
Weird, doesn't really seem to work for me (800K lines, atom shit cpu) Function ran in: 9.590599298477173s Function ran in: 10.747447967529297s Function ran in: 10.441354751586914s
Kevin Roberts
Similar to using in C# and try with ressources in java
Luke Brooks
or spend the time to actually optimize your code using cython