你知道你的电脑 1 秒钟能做多少事情吗?-mile米乐体育
本文由码农网 – 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划!
让我们来看看你有多么了解电脑!所有这些程序的数值都是可变的。你的任务是:在程序花费1秒运行之前猜测它的大概值。
你并不需要猜出一个精确值:选择范围在1和10亿之间。你只要能猜出正确的数量级,就算正确!下面是一些注意事项:
- 如果答案是38,000,那么你选择10,000或100,000,我们就认为都是正确答案。误差只要在10倍范围内就ok:)
- 我们知道不同的计算机有不同的磁盘、网络和cpu速度!我们会告诉运行10次/秒和10万次/秒的代码之间的差别。更新的电脑不会让你的代码运行速度快1000倍:)
- 也就是说,所有这一切都是运行在一台新的拥有一个快速的ssd和一个凑合的网络连接的笔记本电脑上的。 c代码用gcc -o2编译。
祝你好运!
欢迎来到第一个程序!这一个只是让你练练手的:1秒能完成多少循环? (结果可能比你想象得更多!)
猜猜下面的程序每秒执行多少次循环:
#include// 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): pass import sys f(int(sys.argv[1]))
准确答案:68,000,000
当我看着代码的时候,我想的是1毫秒完成多少次——我以为是微不足道的,但事实是,即使是python,你也可以在1毫秒的时间内执行68,000次空循环迭代。
下面让我们来探讨一个更接近现实的用例。在python中字典几乎是无处不在的,那么在1秒时间内我们可以用python添加多少元素呢? 然后再来看一个更复杂的操作——使用python的内置http请求解析器来解析请求。
猜猜下面的程序每秒执行多少次循环:
#!/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 dictionary def f(number): d = {} for i in xrange(number): d[i % 1000] = i import sys f(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 basehttprequesthandler from stringio import stringio class 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 = message request_text = """get / http/1.1 host: localhost:8001 connection: keep-alive accept: text/html,application/xhtml xml,application/xml;q=0.9,image/webp,*/*;q=0.8 upgrade-insecure-requests: 1 user-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.36 accept-encoding: gzip, deflate, sdch accept-language: en-gb,en-us;q=0.8,en;q=0.6 """ def f(number): for _ in range(number): httprequest(request_text) import sys f(int(sys.argv[1]))
准确答案:25,000
我们每秒可以解析25,000个小的http请求!有一件事我要在这里指出的是,这里请求解析的代码是用纯python编写的,而不是c。
接下来,我们要试试下载网页与运行python脚本!提示:少于1亿:)
猜猜下面的程序每秒可以完成多少次http请求:
#!/usr/bin/env python # number to guess: how many times can we # download google.com in a second? from urllib2 import urlopen def f(number): for _ in xrange(number): r = urlopen("http://google.com") r.read() import sys f(int(sys.argv[1]))
准确答案:4
猜猜下面的程序每秒可以执行多少次循环:
#!/bin/bash # number to guess: how many times can we start # the python interpreter in a second? number=$1 for i in $(seq $number); do python -c ''; done
准确答案:77
启动程序实际上昂贵在其本身,而不是启动python。如果我们只是运行/bin/true,那么1秒能做500次,所以看起来运行任何程序只需要大约1毫秒时间。当然,下载网页的快慢很大程度上取决于网页大小,网络连接速度,以及服务器间的距离,不过今天我们不谈网络性能。我的一个朋友说,高性能的网络完成网络往返甚至可能只要250纳秒(!!!),但这是在计算机位置更相邻,硬件更好的情况下。
1秒时间能够在磁盘中写入多少字节?我们都知道写到内存中时速度会更快,但是究竟会快多少呢?对了,下面的代码运行在带有ssd的计算机上。
猜猜下面的程序每秒可以写入多少字节数据:
#!/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 exiting import tempfile import os chunk_size = 1000000 s = "a" * chunk_size def cleanup(f, name): f.flush() os.fsync(f.fileno()) f.close() try: os.remove(name) except: pass def 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 sys f(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 cstringio chunk_size = 1000000 s = "a" * chunk_size def f(number): output = cstringio.stringio() bytes_written = 0 while bytes_written < number: output.write(s) bytes_written = chunk_size import sys f(int(sys.argv[1]))
准确答案:2,000,000,000
下面轮到文件了!有时候,运行一个大型的grep之后,它可以永恒跑下去。在1秒时间内,grep可以搜索多少字节? 请注意,在这么做的时候,grep正在读取的字节已经在内存中。 文件列表同样需要时间!1秒能列出多少文件?
猜猜下面的程序每秒可以搜索多少字节的数据:
#!/bin/bash # number to guess: how many bytes can `grep` # search, unsuccessfully, in a second? # note: the bytes are in memory number=$1 cat /dev/zero | head -c $number | grep blah exit 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
序列化是一个普遍要花费大量时间的地方,让人很蛋疼,特别是如果你反复结束序列化/反序列化相同数据的时候。这里有几个基准:转换64k大小的json格式数据,与同样大小的msgpack格式数据。
猜猜下面的程序每秒可以执行多少次循环:
#!/usr/bin/env python # number to guess: how many times can we parse # 64k of json in a second? import json with open('./setup/protobuf/message.json') as f: message = f.read() def f(number): for _ in xrange(number): json.loads(message) import sys f(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 msgpack with open('./setup/protobuf/message.msgpack') as f: message = f.read() def f(number): for _ in xrange(number): msgpack.unpackb(message) import sys f(int(sys.argv[1]))
准确答案:4,000
数据库。没有任何类似于postgresql花里胡哨的东西,我们做了2份有1000万行数据的sqlite表,一个是有索引的,另一个是未建索引的。
猜猜下面的程序每秒可以执行多少次查询:
#!/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 sqlite3 conn = 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 sys f(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 sqlite3 conn = 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 sys f(int(sys.argv[1]))
准确答案:2
下面要说hash算法!在这里,我们将比较md5和bcrypt。用md5你在1秒时间内可以哈希到相当多的东西,而用bcrypt则不能。
猜猜下面的程序每秒可以哈希多少字节的数据:
#!/usr/bin/env python # number to guess: how many bytes can we md5sum in a second? import hashlib chunk_size = 10000 s = 'a' * chunk_size def f(number): bytes_hashed = 0 h = hashlib.md5() while bytes_hashed < number: h.update(s) bytes_hashed = chunk_size h.digest() import sys f(int(sys.argv[1]))
准确答案:455,000,000
猜猜下面的程序每秒可以哈希多少字节的密码:
#!/usr/bin/env python # number to guess: how many passwords # can we bcrypt in a second? import bcrypt password = 'a' * 100 def f(number): for _ in xrange(number): bcrypt.hashpw(password, bcrypt.gensalt()) import sys f(int(sys.argv[1]))
准确答案:3
接下来,我们要说一说内存访问。 现在的cpu有l1和l2缓存,这比主内存访问速度更快。这意味着,循序访问内存通常比不按顺序访问内存能提供更快的代码。
猜猜下面的程序每秒可以向内存写入多少字节数据:
#include#include // 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 version 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[i] = j; } printf("%d", array[number / 7]); // so that -o2 doesn't optimize out the loop return 0; }
准确答案:376,000,000
猜猜下面的程序每秒可以向内存写入多少字节数据:
#include#include // 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
欢迎大家去试一试,给我们留下宝贵的意见。
译文链接:http://www.codeceo.com/article/1-second-your-computer-do.html 英文原文:do you know how much your computer can do in a second? 翻译作者:码农网 – 小峰 [ 转载必须在正文中标注并保留原文链接、译文链接和译者等信息。]