[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]

python

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: 79
Thread images: 7

File: python-logo.png (82KB, 601x203px) Image search: [Google]
python-logo.png
82KB, 601x203px
Explain this py-cucks:

$ time (gcc sieve.c && ./a.exe 2000000) ; echo "" ; time python sieve.py 2000000
142913828922

real 0m0.346s
user 0m0.185s
sys 0m0.138s

142913828922

real 0m2.343s
user 0m1.859s
sys 0m0.156s
>>
>sieve.c
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

long long sump(int upper);

int main(int argc, char* argv[])
{
if(argc == 1)
{
printf("Specify an upper limit\n");
return 0;
}
int a;
sscanf(argv[1], "%d", &a);
printf("%lld\n", sump(a));
return 1;
}

long long sump(int upper)
{
//TRUE: composite
//FALSE: prime
bool* arr = (bool*) calloc(upper, sizeof(bool));
arr[0] = true;
arr[1] = true;
arr[2] = false;
for(int i = 2; i*i < upper; i++)
{
for(int j = 2*i; j < upper; j += i)
arr[j] = true;
}
//summation
long long ret = 0;
for(int i = 0; i < upper; i++)
if(!arr[i]) ret += i;
return ret;
}
>>
>sieve.py
#! /usr/bin/python
import sys

def main():
if len(sys.argv) == 1:
print "Specify an upper limit"
return
a = int(sys.argv[1])
print sump(a)

def sump(upper):
arr = [False for i in range(upper)]
arr[0] = True
arr[1] = True
arr[2] = False
for i in range(2, int(upper**.5) + 1):
for j in range(2*i, upper, i):
arr[j] = True
ret = 0
for i in range(upper):
if not arr[i]:
ret += i
return ret

if __name__ == "__main__":
main()
>>
>>60980505
compiled vs. interpreted?
>>
>>60980505
>using python directly for numerical computations
Maybe you spend too much time on /g/
>>
range() in python 2 allocates all elements instead of iterating, use xrange() or something like that
>>
142913828922
( gcc sieve.c && ./a.out 2000000; ) 0.14s user 0.02s system 99% cpu 0.170 total

142913828922
( pypy3 sieve.py 2000000; ) 0.51s user 0.06s system 99% cpu 0.566 total
>>
tim@tim-fedora /tmp $ time (gcc -O3 a.c && ./a.out 2000000) ; echo "" ; time python a.py 2000000                                                                                                                  
142913828922
( gcc -O3 a.c && ./a.out 2000000; ) 0,08s user 0,01s system 78% cpu 0,118 total

142913828922
python a.py 2000000 1,35s user 0,03s system 99% cpu 1,389 total

>>
>>60980505
>0m0.346s
I can do this in python:
def sieve(limit):
"""
Sieve of Eratosthenes.
Generate primes up to limit.
"""
if limit >= 2:
yield 2
sieve_limit = int(limit - 1) // 2 # Divide out even range, minus unit 1
x = array.array('B', [1])*sieve_limit
for i in range(sieve_limit):
if x[i]:
p = i*2 + 3
yield p
j = (p*p - 3) >> 1 # sqrt of p minus even range
if j < sieve_limit:
for k in range(j, sieve_limit, p):
x[k] = 0
else:
for k in range(i + 1, sieve_limit):
if x[k]:
yield k*2 + 3
return

if __name__ == "__main__":
print(sum(sieve(2e6)))

$ time python3 sieve_of_Eratosthenes2.py
142913828922

real 0m0.342s
user 0m0.336s
sys 0m0.004s

>>60980537
This is a really shit sieve.
>>
>>60980537
tried with numpy or cython?
>>
File: 1378800235006.gif (921KB, 376x350px) Image search: [Google]
1378800235006.gif
921KB, 376x350px
>>60981056
tim@tim-fedora /tmp $ time python3 b.py                                                                                                                                                                      1 ↵  
Traceback (most recent call last):
File "b.py", line 25, in <module>
print(sum(sieve(2e6)))
File "b.py", line 9, in sieve
x = array.array('B', [1])*sieve_limit
NameError: name 'array' is not defined
python3 b.py 0,10s user 0,02s system 93% cpu 0,123 total


10/10 would run again
>>
>tfw too brainlet to write a good sieve
>>
>>60981121
Forget the import tag
import array

Although you can just use a list, but each element of a list uses 8 bytes, while with a char array you can reduce that by 8, to 1 byte.
>>
>>60981173
tim@tim-fedora /tmp $ time python3 b.py                                                                                                                                                                           
142913828922
python3 b.py 0,27s user 0,00s system 99% cpu 0,280 total


Can you get faster?
>>
>>60981056
>>60981187
why are your times so low?

time python3 botnetsieve.py
142913828922

real 0m0.199s
user 0m0.192s
sys 0m0.004s
>>
>>60981247
Because of
model name    : Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz

i guess
>>
>>60981187
Yes, but barely in native CPython. The problem with a standard unsegmented sieve is the memory requirements for the array. Sieving only the odd range adds the overhead of converting candidates to odd values, in
i*i + 3
; by removing that you can get a speedup, but double the memory requirements. For example, my sieve that I posted requires 4.65GiB to sieve 10**10 ((10^10 bytes / 2 - 1) / 1024^3), but by sieving the entire range, even if incrementing in odd numbers from 3 as in
 for i in range(3, limit + 1, 2)
, you still need an array of size 10**10, meaning 9.31 GiB of memory just to hold it.
>>60981247
Your CPU is better?
>>
>>60980505
Are you retarded? Not one person will realistically argue that python is good for optimization and super speed for programs. Python's purpose is to be expressive, easy, and less code. For most projects, it makes total sense to take the performance hit for a 100000000x better programming experience.
>>
>>60981445
The python code OP posted is almost identical to the C code
>>
>>60981444
>>60981412
yea my cpu is a lot better B^)
>>
should see if tcc runs faster overall.
not really unexpected at all, honestly, we all know c is fast.
adding luajit and some form of c jit (maybe templeos', or just tcc if you want something vaguely serious even though tcc is completely aot) would make a very interesting comparison.
>>
>>60980505
You are running a shitty microbenchmark - that's the explanation.
>>
>>60981445
>For most projects, it makes total sense to take the performance hit for a 100000000x better programming experience.
Why not just use C++ and get the best of both worlds?
>>
>>60980537
>arr[0] = True
>arr[1] = True
>arr[2] = False

>not arr[0:3] = True, True, False

You fucking nigger
>>
>>60981445
>expressive, easy and less code
>has never written anything useful in Python
>>
tim@fakegnus /tmp$ time python3 b.py                                                                                                                                                                              
142913828922
python3 b.py 0,06s user 0,00s system 6% cpu 1,052 total

server performance reporting in
>>
>>60981643
6% system
wew lad
in the trash it goes
also isn't this shit single threaded
kys
>>
>>60980505
Here is a more expensive algorithm, implemented in D, which is 93 (NINETY-THREE) times faster than your pyshit, OP.

Py cucks are cucked forever
>>
>>60982542
OP BTFO
>>
>>60982542
OP's time included compile time and didn't have optimizations turned on.
Retarded deefags can't read.
>>
lel wonders of python

Oh okay, here is a 20 Million sieve. Python is 27 times slower than D without any (ANY) optimization, pytoddler kekek
>>
Turning on the optimization, python is 71 times slower
>>
>>60982542
>>60982752
>>60982870
>being this mad that nobody uses your shitty language
kek
>>
>>60982881
>pytoddler angry his language is 100 times slower
keke

At least your garbo is faster than shitkell
>>
>>60982907
>interpreted languages made with ease of use rather than performance in mind are slower than compiled, performance-focused ones

Next time you'll tell me the sky is blue.
>>
>>60983079
>ease of use
Huh? Ease of use?

Does pytrash even have HOFs?
>>
File: file.png (31KB, 1919x601px) Image search: [Google]
file.png
31KB, 1919x601px
>>60982542
>>60982752
>>60982870
Using OP's measuring method and python code from >>60981056
D is 2.43 times slower than Python.
>>
>>60983114
That's because D's algorithm is more expensive, see >>60982542
>>
>>60983132
>damage control
>>
>>60983134
In what way is that damage control? I literally used that algorithm as a handicap
>>
>>60983104
No idea, but how does that disprove Python's stated aim of focusing on ease of use rather than performance? Why do you think it's an interpreted language to begin with? You're supposed to use it for quick scripts and prototyping, not write a fucking kernel in it.

I know this is probably a troll thread, but come on.
>>
>>60983146
Why don't you write a better one then?
>>
>>60983153
>but how does that disprove Python's stated aim of focusing on ease of use
Higher order functions is meant for ease of use. D has CTFE and mixins for ease of use too. GC can be en/disabled. Compiled programs are supported in all major platforms.
It has no boiler plate, how is D hard to use?
>>
>>60981056
This sieve is producing a constant. It should take O(1) time. What a piece of shit your trash is
>>
>>60980505
idk some compiler optimization or some shit, time to go back to school lad
>>
>>60983114
How the fuck is PyPy so fast? What are the cons to using it over CPython?
>>
>>60983232
>any sieve
>O(1)
Nice bait.
>>
>>60980505
>doing math in python
>doing anything other than short 20 line scripts in scripting languages

wtf are you doing nigger?
>>
>>60983274
I'm not joking. That piece of program posted there does not take any input. It calculates a constant sieve sum, it is an O(1) problem
>>
>>60983265
>What are the cons to using it over CPython
Almost none.
cpyext is now pretty much done so most cpython extensions work fine in pypy, that includes numpy and friends.

One thing I know does not work yet is tensorflow but if you're running tensorflow the expensive stuff is not done in Python anyway.
>>
>>60983265
PyPy is a great project that proves nobody gives a single shit about Python being slow
>>
>>60980505
unless you're doing some academic bullshit, no one uses a 2000000000000000 for loop that contains intensive processing, if you don't like it use c, julia or the retarded golang
>>
>>60983232
the amount of memory required for such a program would be infinite
>>
>>60983328
is it reliable enough? can I run my Django webapp on it without seeing cryptic failures?
>>
>>60983356
You don't get it, the program had 2000000 hardcoded.
The person you're replying to is suggesting to replace the entire thing with print(12272577818052) because they are equivalent.
>>
>>60983295
>That piece of program posted there does not take any input
Literally what the fuck are you talking about? It's a generator function, it takes input as a limit. And as a standard sieve, this limit is used to initialize an array for sieving.
>>
>>60983184

D:

import std.stdio;

void main()
{
writeln("Hello, World!");
}


Python:

print "Hello, World!"
>>
>>60983369
hahaha
>>
>>60983366
If you do see cryptic failures (you almost certainly won't) then that's a bug and you should report it.
If you're using pure Python then it's a drop-in replacement that gives you a free speed boost.
>>
>>60983304
>Almost none.
Well I see it only caught up to 3.5 a couple months ago. There's got to be some downsides, else why wouldn't everyone forget about CPython and move to this?
>>
>>60983366
Probably, just try it out.
>>
>>60983369
I can still produce 12272577818052, by actually computing a sieve and the executable will still be O(1)
>>
>>60983395
Well I didn't say almost for no reason.
I believe they still have money from Mozilla's grant that they are using to catch up with CPython.
The development really sped up recently.

>why wouldn't everyone forget about CPython and move to this
I have no idea but I wish it happened. Guido would probably be salty.
>>
>>60983378
Now grow past hello world, python doesn't have HOFs, it's full of boilerplate
>>
>>60983434
>python doesn't have HOFs
The fuck are you smoking?
>>
>>60983434
And if all you want to write is a hello world program, how is Python not better?
>>
>>60983295
>>60983232
>>60983421
>>60980505
y'all niggas postin in a troll thread
>>
>>60983450
>And if all you want to write is a hello world program
Which I don't
>>
>>60983471
Which I don't care about. If the task at hand is to write a hello world program, explain your reasoning as to why Python is not the better tool for that job.
>>
>>60983421
>I can still produce 12272577818052
What, like compile-time evaluation? So what? The compile time itself won't be O(1) then, and your program won't be reusable, nor will it be a sieve, rather just a print function with a hardcoded value. This is some retarded bait.
>>
>>60983378
>python2
lolwtf
>>
>>60983484
>The compile time itself won't be O(1) then,
I said produced executable
>nor will it be a sieve
It is a sieve
>rather just a print function with a hardcoded value
And you are taking a hard coded input?

Are you pretending to be an idiot?
>>
>>60983490
Fight me, nerd.
>>
>>60983506
dude just post your program or gtfo, there's no use defending yourself like this

pretty sure you're just op samefagging your shitty troll thread
>>
>>60983559
>>60983506
I swear both of you are retarded.
>>
>>60983566
Which board do you think you're on?
>>
>>60983566
ok
>>
File: 1488243967606.jpg (405KB, 900x1200px) Image search: [Google]
1488243967606.jpg
405KB, 900x1200px
>>60983478
Runs in more hardware, doesn't require python runtime
>>
Python is shit
Thread posts: 79
Thread images: 7


[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.