日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
你知道你的電腦 1 秒鐘能做多少事情嗎?

讓我們來看看你有多么了解電腦!所有這些程序的數(shù)值都是可變的。你的任務是:在程序花費1秒運行之前猜測它的大概值。

網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、微信小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了鳩江免費建站歡迎大家使用!

你并不需要猜出一個精確值:選擇范圍在1和10億之間。你只要能猜出正確的數(shù)量級,就算正確!下面是一些注意事項:

  • 如果答案是38,000,那么你選擇10,000或100,000,我們就認為都是正確答案。誤差只要在10倍范圍內(nèi)就ok:)

  • 我們知道不同的計算機有不同的磁盤、網(wǎng)絡和CPU速度!我們會告訴運行10次/秒和10萬次/秒的代碼之間的差別。更新的電腦不會讓你的代碼運行速度快1000倍:)

  • 也就是說,所有這一切都是運行在一臺新的擁有一個快速的SSD和一個湊合的網(wǎng)絡連接的筆記本電腦上的。 C代碼用gcc -O2編譯。

祝你好運!

歡迎來到第一個程序!這一個只是讓你練練手的:1秒能完成多少循環(huán)? (結(jié)果可能比你想象得更多?。?/p>

猜猜下面的程序每秒執(zhí)行多少次循環(huán):

 
 
  1. #include  
  2.  
  3. // Number to guess: How many iterations of 
  4. // this loop can we go through in a second? 
  5.  
  6. int main(int argc, char **argv) { 
  7.     int NUMBER, i, s; 
  8.     NUMBER = atoi(argv[1]); 
  9.  
  10.     for (s = i = 0; i < NUMBER; ++i) { 
  11.         s += 1; 
  12.     } 
  13.  
  14.     return 0; 

準確答案:550,000,000

猜猜下面的程序每秒執(zhí)行多少次循環(huán):

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many iterations of an 
  4. # empty loop can we go through in a second? 
  5.  
  6. def f(NUMBER): 
  7.     for _ in xrange(NUMBER): 
  8.         pass 
  9.  
  10. import sys 
  11. f(int(sys.argv[1])) 

當我看著代碼的時候,我想的是1毫秒完成多少次——我以為是微不足道的,但事實是,即使是Python,你也可以在1毫秒的時間內(nèi)執(zhí)行68,000次空循環(huán)迭代。

下面讓我們來探討一個更接近現(xiàn)實的用例。在Python中字典幾乎是無處不在的,那么在1秒時間內(nèi)我們可以用Python添加多少元素呢?
然后再來看一個更復雜的操作——使用Python的內(nèi)置HTTP請求解析器來解析請求。

猜猜下面的程序每秒執(zhí)行多少次循環(huán):

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many entries can 
  4. # we add to a dictionary in a second? 
  5.  
  6. # Note: we take `i % 1000` to control 
  7. # the size of the dictionary 
  8.  
  9. def f(NUMBER): 
  10.     d = {} 
  11.     for i in xrange(NUMBER): 
  12.         d[i % 1000] = i 
  13.  
  14. import sys 
  15. f(int(sys.argv[1])) 
  16.  
  17. 準確答案:11,000,000 

猜猜下面的程序每秒處理多少次HTTP請求:

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many HTTP requests 
  4. # can we parse in a second? 
  5.  
  6. from BaseHTTPServer import BaseHTTPRequestHandler 
  7. from StringIO import StringIO 
  8.  
  9. class HTTPRequest(BaseHTTPRequestHandler): 
  10.     def __init__(self, request_text): 
  11.         self.rfile = StringIO(request_text) 
  12.         self.raw_requestline = self.rfile.readline() 
  13.         self.error_code = self.error_message = None 
  14.         self.parse_request() 
  15.  
  16.     def send_error(self, code, message): 
  17.         self.error_code = code 
  18.         self.error_message = message 
  19.  
  20. request_text = """GET / HTTP/1.1 
  21. Host: localhost:8001 
  22. Connection: keep-alive 
  23. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
  24. Upgrade-Insecure-Requests: 1 
  25. 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 
  26. Accept-Encoding: gzip, deflate, sdch 
  27. Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 
  28. """ 
  29.  
  30. def f(NUMBER): 
  31.     for _ in range(NUMBER): 
  32.         HTTPRequest(request_text) 
  33.  
  34. import sys 
  35. f(int(sys.argv[1])) 
  36.  
  37. 準確答案:25,000 

我們每秒可以解析25,000個小的HTTP請求!有一件事我要在這里指出的是,這里請求解析的代碼是用純Python編寫的,而不是C。

接下來,我們要試試下載網(wǎng)頁與運行Python腳本!提示:少于1億:)

猜猜下面的程序每秒可以完成多少次HTTP請求:

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many times can we 
  4. # download google.com in a second? 
  5.  
  6. from urllib2 import urlopen 
  7.  
  8. def f(NUMBER): 
  9.     for _ in xrange(NUMBER): 
  10.         r = urlopen("http://google.com") 
  11.         r.read() 
  12.  
  13. import sys 
  14. f(int(sys.argv[1])) 

準確答案:4

猜猜下面的程序每秒可以執(zhí)行多少次循環(huán):

 
 
  1. #!/bin/bash 
  2.  
  3. # Number to guess: How many times can we start 
  4. # the Python interpreter in a second? 
  5.  
  6. NUMBER=$1 
  7.  
  8. for i in $(seq $NUMBER); do 
  9.     python -c ''; 
  10. done 

準確答案:77

啟動程序?qū)嶋H上昂貴在其本身,而不是啟動Python。如果我們只是運行/bin/true,那么1秒能做500次,所以看起來運行任何程序只需要 大約1毫秒時間。當然,下載網(wǎng)頁的快慢很大程度上取決于網(wǎng)頁大小,網(wǎng)絡連接速度,以及服務器間的距離,不過今天我們不談網(wǎng)絡性能。我的一個朋友說,高性能 的網(wǎng)絡完成網(wǎng)絡往返甚至可能只要250納秒(!?。。?,但這是在計算機位置更相鄰,硬件更好的情況下。

1秒時間能夠在磁盤中寫入多少字節(jié)?我們都知道寫到內(nèi)存中時速度會更快,但是究竟會快多少呢?對了,下面的代碼運行在帶有SSD的計算機上。

猜猜下面的程序每秒可以寫入多少字節(jié)數(shù)據(jù):

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many bytes can we write 
  4. # to an output file in a second? 
  5. # Note: we make sure everything is sync'd to disk 
  6. # before exiting 
  7. import tempfile 
  8. import os 
  9.  
  10. CHUNK_SIZE = 1000000 
  11. s = "a" * CHUNK_SIZE 
  12.  
  13. def cleanup(f, name): 
  14.     f.flush() 
  15.     os.fsync(f.fileno()) 
  16.     f.close() 
  17.     try: 
  18.         os.remove(name) 
  19.     except: 
  20.         pass 
  21.  
  22. def f(NUMBER): 
  23.     name = './out' 
  24.     f = open(name, 'w') 
  25.     bytes_written = 0 
  26.     while bytes_written < NUMBER: 
  27.         f.write(s) 
  28.         bytes_written += CHUNK_SIZE 
  29.     cleanup(f, name) 
  30.  
  31. import sys 
  32. f(int(sys.argv[1])) 

準確答案:342,000,000

猜猜下面的程序每秒可以寫入多少字節(jié)數(shù)據(jù):

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many bytes can we write 
  4. # to a string in memory in a second? 
  5.  
  6. import cStringIO 
  7.  
  8. CHUNK_SIZE = 1000000 
  9. s = "a" * CHUNK_SIZE 
  10.  
  11. def f(NUMBER): 
  12.     output = cStringIO.StringIO() 
  13.     bytes_written = 0 
  14.     while bytes_written < NUMBER: 
  15.         output.write(s) 
  16.         bytes_written += CHUNK_SIZE 
  17.  
  18. import sys 
  19. f(int(sys.argv[1])) 

準確答案:2,000,000,000

下面輪到文件了!有時候,運行一個大型的grep之后,它可以永恒跑下去。在1秒時間內(nèi),grep可以搜索多少字節(jié)?
請注意,在這么做的時候,grep正在讀取的字節(jié)已經(jīng)在內(nèi)存中。
文件列表同樣需要時間!1秒能列出多少文件?

猜猜下面的程序每秒可以搜索多少字節(jié)的數(shù)據(jù):

 
 
  1. #!/bin/bash 
  2.  
  3. # Number to guess: How many bytes can `grep` 
  4. # search, unsuccessfully, in a second? 
  5. # Note: the bytes are in memory 
  6.  
  7. NUMBER=$1 
  8.  
  9. cat /dev/zero | head -c $NUMBER | grep blah 
  10. exit 0 
  11.  
  12. 準確答案:2,000,000,000 
  13.  
  14. 猜猜下面的程序每秒可以列出多少文件: 
  15.  
  16. #!/bin/bash 
  17.  
  18. # Number to guess: How many files can `find` list in a second? 
  19. # Note: the files will be in the filesystem cache. 
  20.  
  21. find / -name '*' 2> /dev/null | head -n $1 > /dev/null 

準確答案:325,000

序列化是一個普遍要花費大量時間的地方,讓人很蛋疼,特別是如果你反復結(jié)束序列化/反序列化相同數(shù)據(jù)的時候。這里有幾個基準:轉(zhuǎn)換64K大小的JSON格式數(shù)據(jù),與同樣大小的msgpack格式數(shù)據(jù)。

猜猜下面的程序每秒可以執(zhí)行多少次循環(huán):

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many times can we parse 
  4. # 64K of JSON in a second? 
  5.  
  6. import json 
  7.  
  8. with open('./setup/protobuf/message.json') as f: 
  9.     message = f.read() 
  10.  
  11. def f(NUMBER): 
  12.     for _ in xrange(NUMBER): 
  13.         json.loads(message) 
  14.  
  15. import sys 
  16. f(int(sys.argv[1])) 

準確答案:449

猜猜下面的程序每秒可以執(zhí)行多少次循環(huán):

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many times can we parse 
  4. # 46K of msgpack data in a second? 
  5.  
  6. import msgpack 
  7.  
  8. with open('./setup/protobuf/message.msgpack') as f: 
  9.     message = f.read() 
  10.  
  11. def f(NUMBER): 
  12.     for _ in xrange(NUMBER): 
  13.         msgpack.unpackb(message) 
  14.  
  15. import sys 
  16. f(int(sys.argv[1])) 

準確答案:4,000

數(shù)據(jù)庫。沒有任何類似于PostgreSQL花里胡哨的東西,我們做了2份有1000萬行數(shù)據(jù)的SQLite表,一個是有索引的,另一個是未建索引的。

猜猜下面的程序每秒可以執(zhí)行多少次查詢:

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many times can we 
  4. # select a row from an **indexed** table with 
  5. # 10,000,000 rows? 
  6.  
  7. import sqlite3 
  8.  
  9. conn = sqlite3.connect('./indexed_db.sqlite') 
  10. c = conn.cursor() 
  11. def f(NUMBER): 
  12.     query = "select * from my_table where key = %d" % 5 
  13.     for i in xrange(NUMBER): 
  14.         c.execute(query) 
  15.         c.fetchall() 
  16.  
  17. import sys 
  18. f(int(sys.argv[1])) 

準確答案:53,000

猜猜下面的程序每秒執(zhí)行多少次查詢:

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many times can we 
  4. # select a row from an **unindexed** table with 
  5. # 10,000,000 rows? 
  6.  
  7. import sqlite3 
  8.  
  9. conn = sqlite3.connect('./unindexed_db.sqlite') 
  10. c = conn.cursor() 
  11. def f(NUMBER): 
  12.     query = "select * from my_table where key = %d" % 5 
  13.     for i in xrange(NUMBER): 
  14.         c.execute(query) 
  15.         c.fetchall() 
  16.  
  17. import sys 
  18. f(int(sys.argv[1])) 

準確答案:2

下面要說Hash算法!在這里,我們將比較MD5和bcrypt。用MD5你在1秒時間內(nèi)可以哈希到相當多的東西,而用bcrypt則不能。

猜猜下面的程序每秒可以哈希多少字節(jié)的數(shù)據(jù):

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many bytes can we md5sum in a second? 
  4.  
  5. import hashlib 
  6.  
  7. CHUNK_SIZE = 10000 
  8. s = 'a' * CHUNK_SIZE 
  9.  
  10. def f(NUMBER): 
  11.     bytes_hashed = 0 
  12.     h = hashlib.md5() 
  13.     while bytes_hashed < NUMBER: 
  14.         h.update(s) 
  15.         bytes_hashed += CHUNK_SIZE 
  16.     h.digest() 
  17. import sys 
  18. f(int(sys.argv[1])) 

準確答案:455,000,000

猜猜下面的程序每秒可以哈希多少字節(jié)的密碼:

 
 
  1. #!/usr/bin/env python 
  2.  
  3. # Number to guess: How many passwords 
  4. # can we bcrypt in a second? 
  5.  
  6. import bcrypt 
  7.  
  8. password = 'a' * 100 
  9.  
  10. def f(NUMBER): 
  11.     for _ in xrange(NUMBER): 
  12.         bcrypt.hashpw(password, bcrypt.gensalt()) 
  13.  
  14. import sys 
  15. f(int(sys.argv[1])) 

準確答案:3

接下來,我們要說一說內(nèi)存訪問。 現(xiàn)在的CPU有L1和L2緩存,這比主內(nèi)存訪問速度更快。這意味著,循序訪問內(nèi)存通常比不按順序訪問內(nèi)存能提供更快的代碼。

猜猜下面的程序每秒可以向內(nèi)存寫入多少字節(jié)數(shù)據(jù):

 
 
  1. #include  
  2. #include  
  3.  
  4. // Number to guess: How big of an array (in bytes) 
  5. // can we allocate and fill in a second? 
  6.  
  7. // this is intentionally more complicated than it needs to be 
  8. // so that it matches the out-of-order version 
  9.  
  10. int main(int argc, char **argv) { 
  11.     int NUMBER, i; 
  12.     NUMBER = atoi(argv[1]); 
  13.  
  14.     char* array = malloc(NUMBER); 
  15.     int j = 1; 
  16.     for (i = 0; i < NUMBER; ++i) { 
  17.         j = j * 2; 
  18.         if (j > NUMBER) { 
  19.             j = j - NUMBER; 
  20.         } 
  21.         array[i] = j; 
  22.     } 
  23.  
  24.     printf("%d", array[NUMBER / 7]); 
  25.     // so that -O2 doesn't optimize out the loop 
  26.  
  27.     return 0; 

準確答案:376,000,000

猜猜下面的程序每秒可以向內(nèi)存寫入多少字節(jié)數(shù)據(jù):

 
 
  1. #include  
  2. #include  
  3.  
  4. // Number to guess: How big of an array (in bytes) 
  5. // can we allocate and fill with 5s in a second? 
  6. // The catch: We do it out of order instead of in order. 
  7. int main(int argc, char **argv) { 
  8.     int NUMBER, i; 
  9.     NUMBER = atoi(argv[1]); 
  10.  
  11.     char* array = malloc(NUMBER); 
  12.     int j = 1; 
  13.     for (i = 0; i < NUMBER; ++i) { 
  14.         j = j * 2; 
  15.         if (j > NUMBER) { 
  16.             j = j - NUMBER; 
  17.         } 
  18.         array[j] = j; 
  19.     } 
  20.  
  21.     printf("%d", array[NUMBER / 7]); 
  22.     // so that -O2 doesn't optimize out the loop 
  23.  
  24.     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?


分享題目:你知道你的電腦 1 秒鐘能做多少事情嗎?
轉(zhuǎn)載來于:http://www.dlmjj.cn/article/dhpogep.html