[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

What the fuck is wrong with python? Test file: 33752686 lines

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 26
Thread images: 2

File: Python-logo-notext.svg.png (72KB, 1024x1024px) Image search: [Google]
Python-logo-notext.svg.png
72KB, 1024x1024px
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
>>
>>57863620
real    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!
>>
File: 1446653548222.png (54KB, 625x626px) Image search: [Google]
1446653548222.png
54KB, 625x626px
>>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 do
with 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:t
newlist = []
with open('out') as f:
for line in f:
newlist.append(f.read().upper())
>>
>>57864181
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 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 of
File.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
Thread posts: 26
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.