cpython

Python hasattr vs getattr

☆樱花仙子☆ 提交于 2019-11-30 12:18:42
I have been reading lately some tweets and the python documentation about hasattr and it says: hasattr(object, name) The arguments are an object and a string. The result is True if the string is the name of >> one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.) There is a motto in Python that says that is Easier to ask for forgiveness than permission where I usually agree. I tried to do a performance test in this case with a really simple python code: import timeit definition="""\ class A

Why does my Sieve of Eratosthenes work faster with integers than with booleans?

丶灬走出姿态 提交于 2019-11-30 11:43:09
I wrote a simple Sieve of Eratosthenes, which uses a list of ones and turns them into zeros if not prime, like so: def eSieve(n): #Where m is fixed-length list of all integers up to n '''Creates a list of primes less than or equal to n''' m = [1]*(n+1) for i in xrange(2,int((n)**0.5)+1): if m[i]: for j in xrange(i*i,n+1,i): m[j]=0 return [i for i in xrange(2,n) if m[i]] I tested the speed it ran with %timeit and got: #n: t #10**1: 7 μs #10**2: 26.6 μs #10**3: 234 μs #10**4: 2.46 ms #10**5: 26.4 ms #10**6: 292 ms #10**7: 3.27 s I assumed, if I changed [1] and 0 to booleans, it would run faster.

Why does this Python script run 4x slower on multiple cores than on a single core

萝らか妹 提交于 2019-11-30 08:36:11
问题 I'm trying to understand how CPython's GIL works and what are the differences between GIL in CPython 2.7.x and CPython 3.4.x. I'm using this code for benchmarking: from __future__ import print_function import argparse import resource import sys import threading import time def countdown(n): while n > 0: n -= 1 def get_time(): stats = resource.getrusage(resource.RUSAGE_SELF) total_cpu_time = stats.ru_utime + stats.ru_stime return time.time(), total_cpu_time, stats.ru_utime, stats.ru_stime def

Why is string comparison so fast in python?

家住魔仙堡 提交于 2019-11-30 06:06:14
I became curious to understand the internals of how string comparison works in python when I was solving the following example algorithm problem: Given two strings, return the length of the longest common prefix Solution 1: charByChar My intuition told me that the optimal solution would be to start with one cursor at the beginning of both words and iterate forward until the prefixes no longer match. Something like def charByChar(smaller, bigger): assert len(smaller) <= len(bigger) for p in range(len(smaller)): if smaller[p] != bigger[p]: return p return len(smaller) To simplify the code, the

Obfuscating python bytecode through interpreter mutation

强颜欢笑 提交于 2019-11-30 05:42:53
Actually, Dropbox made it very well, they were able to secure their desktop application made in python; I researched this a lot, but no good solution better than obfuscation, which is not very secure way to go, and you will end up seeing your code uploaded somewhere. I listened to a session made by Giovanni Bajo (the PyInstaller founder), he said Dropbox does this: Bytecode-scrambling by recompiling your CPython's interpreter, and by this, standard CPython interpreter will not be able to run it, only the recompiled cpython interpreter. All what you need to do is to shuffle the numbers below

Python 3.5 vs. 3.6 what made “map” slower compared to comprehensions

雨燕双飞 提交于 2019-11-30 05:11:10
I sometimes used map if there was a function/method that was written in C to get a bit extra performance. However I recently revisited some of my benchmarks and noticed that the relative performance (compared to a similar list comprehension) drastically changed between Python 3.5 and 3.6. That's not the actual code but just a minimal sample that illustrates the difference: import random lst = [random.randint(0, 10) for _ in range(100000)] assert list(map((5).__lt__, lst)) == [5 < i for i in lst] %timeit list(map((5).__lt__, lst)) %timeit [5 < i for i in lst] I realize that it's not a good idea

Why is deque implemented as a linked list instead of a circular array?

帅比萌擦擦* 提交于 2019-11-30 04:42:26
CPython deque is implemented as a doubly-linked list of 64-item sized "blocks" (arrays). The blocks are all full, except for the ones at either end of the linked list. IIUC, the blocks are freed when a pop / popleft removes the last item in the block; they are allocated when append / appendleft attempts to add a new item and the relevant block is full. I understand the listed advantages of using a linked list of blocks rather than a linked list of items: reduce memory cost of pointers to prev and next in every item reduce runtime cost of doing malloc / free for every item added/removed improve

Migrating from CPython to Jython

坚强是说给别人听的谎言 提交于 2019-11-30 04:37:53
I'm considering moving my code (around 30K LOC) from CPython to Jython, so that I could have better integration with my java code. Is there a checklist or a guide I should look at, to help my with the migration? Does anyone have experience with doing something similar? From reading the Jython site , most of the problems seem too obscure to bother me. I did notice that: thread safety is an issue Unicode support seems to be quite different, which may be a problem for me mysqldb doesn't work and needs to be replaced with zxJDBC Anything else? Related question: What are some strategies to write

Why is string's startswith slower than in?

廉价感情. 提交于 2019-11-30 04:10:13
Surprisingly, I find startswith is slower than in : In [10]: s="ABCD"*10 In [11]: %timeit s.startswith("XYZ") 1000000 loops, best of 3: 307 ns per loop In [12]: %timeit "XYZ" in s 10000000 loops, best of 3: 81.7 ns per loop As we all know, the in operation needs to search the whole string and startswith just needs to check the first few characters, so startswith should be more efficient. When s is big enough, startswith is faster: In [13]: s="ABCD"*200 In [14]: %timeit s.startswith("XYZ") 1000000 loops, best of 3: 306 ns per loop In [15]: %timeit "XYZ" in s 1000000 loops, best of 3: 666 ns per

hanlp在Python环境中的安装失败后的解决方法

爷,独闯天下 提交于 2019-11-30 03:17:39
Hanlp是由一系列模型与算法组成的javag工具包,目标是普及自然语言处理再生环境中的应用。有很多人在安装hanlp的时候会遇到安装失败的情况,下面就是某大神的分享的在python环境中安装失败的解决方法,大家可以借鉴学习以下! 由于要使用hanlp进行分词,而我们的环境是python环境所以得安装pyhanlp,但是安装过程总是出现这样的问题 看上去感觉是缺少了visual c++环境,于是安装visual c++,可查看这个博客www.hankcs.com/nlp/python-calls-hanlp.html 安装完后发现问题并没有解决,初步怀疑应该是 jpype1没有安装成功,于是使用pip install jpype1发现果然失败,最终手动安装pip install D:\soft\JPype1-0.6.2-cp36-cp36m-win_amd64.whl 安装成功。 安装包地址:www.lfd.uci.edu/~gohlke/pythonlibs/#jpype 注意如果出现 JPype1-0.6.3-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform.可看如下搭配更换版本: cp27 → CPython 2.7 cp34 → CPython 3.4 cp35 → CPython 3.5