让我们来看看你有多么了解电脑!所有这些程序的数值都是可变的。你的任务是:在程序花费1秒运行之前猜测它的大概值。 你并不需要猜出一个精确值:选择范围在1和10亿之间。你只要能猜出正确的数量级,就算正确!下面是一些注意事项:
祝你好运!
猜猜下面的程序每秒执行多少次循环: #include <stdlib.h>// Number to guess: How many iterations of// this loop can we go through in a second?int main(int argc, char **argv) { int NUMBER, i, s; NUMBER = atoi(argv[1]); for (s = i = 0; i < NUMBER; ++i) { s += 1; } return 0;} 准确答案:550,000,000 猜猜下面的程序每秒执行多少次循环: #!/usr/bin/env python# Number to guess: How many iterations of an# empty loop can we go through in a second?def f(NUMBER): for _ in xrange(NUMBER): passimport sysf(int(sys.argv[1])) 准确答案:68,000,000 当我看着代码的时候,我想的是1毫秒完成多少次——我以为是微不足道的,但事实是,即使是Python,你也可以在1毫秒的时间内执行68,000次空循环迭代。
猜猜下面的程序每秒执行多少次循环: #!/usr/bin/env python# Number to guess: How many entries can# we add to a dictionary in a second?# Note: we take `i % 1000` to control# the size of the dictionarydef f(NUMBER): d = {} for i in xrange(NUMBER): d[i % 1000] = iimport sysf(int(sys.argv[1])) 准确答案:11,000,000 猜猜下面的程序每秒处理多少次HTTP请求: #!/usr/bin/env python# Number to guess: How many HTTP requests# can we parse in a second?from BaseHTTPServer import BaseHTTPRequestHandlerfrom StringIO import StringIOclass HTTPRequest(BaseHTTPRequestHandler): def __init__(self, request_text): self.rfile = StringIO(request_text) self.raw_requestline = self.rfile.readline() self.error_code = self.error_message = None self.parse_request() def send_error(self, code, message): self.error_code = code self.error_message = messagerequest_text = """GET / HTTP/1.1Host: localhost:8001Connection: keep-aliveAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36Accept-Encoding: gzip, deflate, sdchAccept-Language: en-GB,en-US;q=0.8,en;q=0.6"""def f(NUMBER): for _ in range(NUMBER): HTTPRequest(request_text)import sysf(int(sys.argv[1])) 准确答案:25,000 我们每秒可以解析25,000个小的HTTP请求!有一件事我要在这里指出的是,这里请求解析的代码是用纯Python编写的,而不是C。
猜猜下面的程序每秒可以完成多少次HTTP请求: #!/usr/bin/env python# Number to guess: How many times can we# download google.com in a second?from urllib2 import urlopendef f(NUMBER): for _ in xrange(NUMBER): r = urlopen("http://google.com") r.read()import sysf(int(sys.argv[1])) 准确答案:4 猜猜下面的程序每秒可以执行多少次循环: #!/bin/bash# Number to guess: How many times can we start# the Python interpreter in a second?NUMBER=$1for i in $(seq $NUMBER); do python -c '';done 准确答案:77 启动程序实际上昂贵在其本身,而不是启动Python。如果我们只是运行/bin/true,那么1秒能做500次,所以看起来运行任何程序只需要大约1毫秒时间。当然,下载网页的快慢很大程度上取决于网页大小,网络连接速度,以及服务器间的距离,不过今天我们不谈网络性能。我的一个朋友说,高性能的网络完成网络往返甚至可能只要250纳秒(!!!),但这是在计算机位置更相邻,硬件更好的情况下。
猜猜下面的程序每秒可以写入多少字节数据: #!/usr/bin/env python# Number to guess: How many bytes can we write# to an output file in a second?# Note: we make sure everything is sync'd to disk# before exitingimport tempfileimport osCHUNK_SIZE = 1000000s = "a" * CHUNK_SIZEdef cleanup(f, name): f.flush() os.fsync(f.fileno()) f.close() try: os.remove(name) except: passdef f(NUMBER): name = './out' f = open(name, 'w') bytes_written = 0 while bytes_written < NUMBER: f.write(s) bytes_written += CHUNK_SIZE cleanup(f, name)import sysf(int(sys.argv[1])) 准确答案:342,000,000 猜猜下面的程序每秒可以写入多少字节数据: #!/usr/bin/env python# Number to guess: How many bytes can we write# to a string in memory in a second?import cStringIOCHUNK_SIZE = 1000000s = "a" * CHUNK_SIZEdef f(NUMBER): output = cStringIO.StringIO() bytes_written = 0 while bytes_written < NUMBER: output.write(s) bytes_written += CHUNK_SIZEimport sysf(int(sys.argv[1])) 准确答案:2,000,000,000
猜猜下面的程序每秒可以搜索多少字节的数据: #!/bin/bash # Number to guess: How many bytes can `grep`# search, unsuccessfully, in a second?# Note: the bytes are in memoryNUMBER=$1cat /dev/zero | head -c $NUMBER | grep blahexit 0 准确答案:2,000,000,000 猜猜下面的程序每秒可以列出多少文件: #!/bin/bash# Number to guess: How many files can `find` list in a second?# Note: the files will be in the filesystem cache.find / -name '*' 2> /dev/null | head -n $1 > /dev/null 准确答案:325,000
猜猜下面的程序每秒可以执行多少次循环: #!/usr/bin/env python# Number to guess: How many times can we parse# 64K of JSON in a second?import jsonwith open('./setup/protobuf/message.json') as f: message = f.read()def f(NUMBER): for _ in xrange(NUMBER): json.loads(message)import sysf(int(sys.argv[1])) 准确答案:449 猜猜下面的程序每秒可以执行多少次循环: #!/usr/bin/env python# Number to guess: How many times can we parse# 46K of msgpack data in a second?import msgpackwith open('./setup/protobuf/message.msgpack') as f: message = f.read()def f(NUMBER): for _ in xrange(NUMBER): msgpack.unpackb(message)import sysf(int(sys.argv[1])) 准确答案:4,000
猜猜下面的程序每秒可以执行多少次查询: #!/usr/bin/env python# Number to guess: How many times can we# select a row from an **indexed** table with # 10,000,000 rows?import sqlite3conn = sqlite3.connect('./indexed_db.sqlite')c = conn.cursor()def f(NUMBER): query = "select * from my_table where key = %d" % 5 for i in xrange(NUMBER): c.execute(query) c.fetchall()import sysf(int(sys.argv[1])) 准确答案:53,000 猜猜下面的程序每秒执行多少次查询: #!/usr/bin/env python# Number to guess: How many times can we# select a row from an **unindexed** table with # 10,000,000 rows?import sqlite3conn = sqlite3.connect('./unindexed_db.sqlite')c = conn.cursor()def f(NUMBER): query = "select * from my_table where key = %d" % 5 for i in xrange(NUMBER): c.execute(query) c.fetchall()import sysf(int(sys.argv[1])) 准确答案:2
猜猜下面的程序每秒可以哈希多少字节的数据: #!/usr/bin/env python# Number to guess: How many bytes can we md5sum in a second?import hashlibCHUNK_SIZE = 10000s = 'a' * CHUNK_SIZEdef f(NUMBER): bytes_hashed = 0 h = hashlib.md5() while bytes_hashed < NUMBER: h.update(s) bytes_hashed += CHUNK_SIZE h.digest()import sysf(int(sys.argv[1])) 准确答案:455,000,000 猜猜下面的程序每秒可以哈希多少字节的密码: #!/usr/bin/env python# Number to guess: How many passwords# can we bcrypt in a second?import bcryptpassword = 'a' * 100def f(NUMBER): for _ in xrange(NUMBER): bcrypt.hashpw(password, bcrypt.gensalt())import sysf(int(sys.argv[1])) 准确答案:3
猜猜下面的程序每秒可以向内存写入多少字节数据: #include <stdlib.h>#include <stdio.h>// Number to guess: How big of an array (in bytes)// can we allocate and fill in a second?// this is intentionally more complicated than it needs to be// so that it matches the out-of-order versionint main(int argc, char **argv) { int NUMBER, i; NUMBER = atoi(argv[1]); char* array = malloc(NUMBER); int j = 1; for (i = 0; i < NUMBER; ++i) { j = j * 2; if (j > NUMBER) { j = j - NUMBER; } array[i] = j; } printf("%d", array[NUMBER / 7]); // so that -O2 doesn't optimize out the loop return 0;} 准确答案:376,000,000 猜猜下面的程序每秒可以向内存写入多少字节数据: #include <stdlib.h>#include <stdio.h>// Number to guess: How big of an array (in bytes)// can we allocate and fill with 5s in a second?// The catch: We do it out of order instead of in order.int main(int argc, char **argv) { int NUMBER, i; NUMBER = atoi(argv[1]); char* array = malloc(NUMBER); int j = 1; for (i = 0; i < NUMBER; ++i) { j = j * 2; if (j > NUMBER) { j = j - NUMBER; } array[j] = j; } printf("%d", array[NUMBER / 7]); // so that -O2 doesn't optimize out the loop return 0;} 准确答案:68,000,000 欢迎大家去试一试,给我们留下宝贵的意见。 |