今天看到一个帖子说 python 中 while 1: pass 要比 while True: pass 速度更快。
第一感觉是有点反直觉。 while 1: pass 貌似应等价于 while bool(1): pass , 怎么也要比 while True: pass 多一次转换的调用。
于是做了一个简单的实验,
In [1]: timeit while True: break
10000000 loops, best of 3: 109 ns per loop
| package puzzle; | |
| import java.util.ArrayList; | |
| import java.util.concurrent.locks.ReentrantLock; | |
| public abstract class Buffer<T> { | |
| private ArrayList<T> _items = null; | |
| private ArrayList<T> _prepare = null; | |
| local ffi = require "ffi" | |
| local tick | |
| local n = 8000 | |
| local x = {} | |
| for i = 1, 8000 do | |
| x[i] = i | |
| end |
| import ssl | |
| import socket | |
| context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) | |
| context.set_npn_protocols(["http/1.1", "spdy/3"]) | |
| sock = socket.socket() | |
| ssl_sock = context.wrap_socket(sock) | |
| ssl_sock.connect(("www.google.com", 443)) |
| #!/usr/bin/env python | |
| import sys | |
| import re | |
| import pwd | |
| import os | |
| ptn = re.compile("refs/heads/(.*)") | |
| allows_only = { | |
| "master": ["yuanye4"] | |
| } |
| import subprocess | |
| p = subprocess.Popen(["redis-cli", "--pipe"], | |
| stdin=subprocess.PIPE) | |
| for x in xrange(0, 100): | |
| # write to redis-cli stdin in redis protocol | |
| p.stdin.write("set %s %s\r\n" % (x, x + 1) | |
| p.stdin.close() |
今天看到一个帖子说 python 中 while 1: pass 要比 while True: pass 速度更快。
第一感觉是有点反直觉。 while 1: pass 貌似应等价于 while bool(1): pass , 怎么也要比 while True: pass 多一次转换的调用。
于是做了一个简单的实验,
In [1]: timeit while True: break
10000000 loops, best of 3: 109 ns per loop
| local ffi = require("ffi") | |
| ffi.cdef[[ | |
| typedef unsigned char uuid_t[16]; | |
| void uuid_generate(uuid_t out); | |
| void uuid_unparse(const uuid_t uu, char *out); | |
| ]] | |
| local libuuid = ffi.os == "OSX" and ffi.C or ffi.load("uuid") |
| import gevent.monkey | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| logger.info("Gevent monkey patch is applied") | |
| gevent.monkey.patch_all() | |
| # RLock of sentry log handler may be created before applying gevent monkey | |
| # patch, which will lead to a deadlock. Replace it with a gevent RLock |
| def print_frames(greenlet): | |
| f = greenlet.gr_frame | |
| while f: | |
| print f.f_code, f.f_locals | |
| f = f.f_back |
| function scoped(...) | |
| local args = {...} | |
| local alias, f | |
| if #args == 1 then | |
| f = args[1] | |
| alias = "defer" | |
| elseif # args == 2 then | |
| f = args[2] |