我已经是一个超过10年的Perl家伙,但是一位朋友说服我尝试使用Python并告诉我它比Perl快多少.所以只是为了踢,我把我用Perl编写的应用程序移植到Python中,发现它运行速度慢了3倍.最初,我的朋友告诉我,我一定做错了,所以我重写并重构,直到我不能重写和重构......它仍然慢得多.所以我做了一个简单的测试:
i = 0
j = 0
while (i < 100000000):
i = i + 1
j = j + 1
print j
Run Code Online (Sandbox Code Playgroud)
$ time python python.py
100000000真正的0m48.100s
用户0m45.633s
sys 0m0.043s
my $i = 0;
my $j = 0;
while ($i < 100000000) {
++$i; # also tested $i = $i + 1 to be fair, same result
++$j;
}
print $j;
Run Code Online (Sandbox Code Playgroud)
$ time perl perl.pl
100000000实际0m24.757s
用户0m22.341s
sys 0m0.029s
慢了两倍,这似乎没有反映出我见过的任何基准测试......我的安装有问题还是Python真的比Perl慢得多?
这是我在我的VPS主机中提供python 2.4的常规代码
def mail(receiver,Message):
import smtplib
try:
s=smtplib.SMTP()
s.connect("smtp.gmail.com",465)
s.login("email@gmail.com", "password")
s.sendmail("email@gmail.com", receiver, Message)
except Exception,R:
return R
Run Code Online (Sandbox Code Playgroud)
但不幸的是返回此消息!:
SMTP AUTH extension not supported by server.
在我安装python 2.7的计算机中,我找到了解决方案并且它的工作非常好,这里是这段代码:
def mail(T,M):
import smtplib
try:
s=smtplib.SMTP_SSL()
s.connect("smtp.gmail.com",465)
s.login("xxxxx@gmail.com","your_password")
s.sendmail("xxxxx@gmail.com", T, M)
except Exception,R:
print R
Run Code Online (Sandbox Code Playgroud)
但是在安装了python 2.4的VPS中没有SMTP_SSL()并返回此消息 'module' object has no attribute 'SMTP_SSL'
此外,我试图在VPS升级我的python,但发生的事情是损坏整个python,这意味着python根本不起作用.
看看这段代码,
img = cv2.imread("image2.jpg",0)
img_str = cv2.imencode('.jpg', img)[1] #Encodes and stores in buffer
print(img_str)
#for i,item in enumerate(img_str): } THIS
# img_str[i] = 255-item } IS CONFUSING
nparr = np.frombuffer(img_str, np.uint8)
img2 = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
Run Code Online (Sandbox Code Playgroud)
我的问题是没有这个,
#for i,item in enumerate(img_str): } THIS
# img_str[i] = 255-item } IS CONFUSING
Run Code Online (Sandbox Code Playgroud)
代码运行良好 imdecode 返回相同!但是当我取消注释它 imdecode 返回None
如果缓冲区太短或包含无效数据,则返回空矩阵/图像。
使 imdecode 返回 none 的无效数据究竟是什么?还是其他错误?
检查了 cpython 源代码中的Secrets和uuid4。两者似乎都在使用 os.urandom。
#uuid.py
def uuid4():
"""Generate a random UUID."""
return UUID(bytes=os.urandom(16), version=4)
#secrets.py
def token_bytes(nbytes=None):
"""Return a random byte string containing *nbytes* bytes.
If *nbytes* is ``None`` or not supplied, a reasonable
default is used.
>>> token_bytes(16) #doctest:+SKIP
b'\\xebr\\x17D*t\\xae\\xd4\\xe3S\\xb6\\xe2\\xebP1\\x8b'
"""
if nbytes is None:
nbytes = DEFAULT_ENTROPY
return _sysrand.randbytes(nbytes)
# This is code for randbytes in SystemRandom in random
def randbytes(self, n):
"""Generate n random bytes."""
# os.urandom(n) fails with ValueError for n < …Run Code Online (Sandbox Code Playgroud) 我有这个
s = ['son','abc','pro','bro']
b = ['son','bro']
c = ['pro','quo']
Run Code Online (Sandbox Code Playgroud)
预期的输出是这样的。index(item_in_s)如果它出现在 list 中,则输出中的项目所在的位置b。或者index(item_in_s)+10如果一个项目在c.
[0,12,3]
Run Code Online (Sandbox Code Playgroud)
我试过这个:
index_list = [s.index(item) if item in b else s.index(item)+10 if item in c for item in s]
print(index)
Run Code Online (Sandbox Code Playgroud)
但显然这是一个语法错误。所以我试过这个:
index_list = [s.index(item) if item in b else s.index(item)+10 for item in s if item in c]
print(index)
Run Code Online (Sandbox Code Playgroud)
输出:
[12]
Run Code Online (Sandbox Code Playgroud)
这只是改变了整个逻辑。虽然我可以做到这一点
fin = [s.index(item) if item in b else s.index(item)+10 if item in c else '' for item …Run Code Online (Sandbox Code Playgroud) 说我有绳子,
string1 = 'Hello how are you'
string2 = 'are you doing now?'
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的
Hello how are you doing now?
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用re和字符串搜索的不同方式。(最长公共子串问题)
但是有没有什么简单的方法(或库)可以在 python 中做到这一点?
为了清楚起见,我将再添加一组测试字符串!
string1 = 'This is a nice ACADEMY'
string2 = 'DEMY you know!'
Run Code Online (Sandbox Code Playgroud)
结果是!,
'This is a nice ACADEMY you know!'
Run Code Online (Sandbox Code Playgroud) 这是我的示例代码.
#include <stdio.h>
#include <string.h>
int main() {
char a[3] = { 'H', 'E', 'L', 'L', 'O', '\0' };
printf("Length is %zd ", strlen(a));
}
Run Code Online (Sandbox Code Playgroud)
我知道这会产生以下警告!
test.c:5:26: warning: excess elements in array initializer
char a[3] = {'H','E','L','L','O','\0'};
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,如果我指定大小为a[6]或大于我的输出的实际大小.
对于 char a[100] = {'H','E','L','L','O','\0'};
输出:
Length is 5
Run Code Online (Sandbox Code Playgroud)
对于 char a[10] = {'H','E','L','L','O','\0'};
输出:
Length is 5
Run Code Online (Sandbox Code Playgroud)
对于任何等于或大于数组大小的东西,我都得到正确的输出.
但是当我给出比实际尺寸更小的东西时,我总是得到 6作为输出.
为char a[5] = {'H','E','L','L','O','\0'};或为a[4]或a[3]或或a[2]它总是
Length is 6
虽然为 …
python ×5
python-2.7 ×2
python-3.x ×2
string ×2
c ×1
cryptography ×1
image ×1
linux ×1
list ×1
opencv ×1
performance ×1
perl ×1
python-2.4 ×1
random ×1
smtp ×1
uuid ×1
vps ×1