自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

你知道你的電腦 1 秒鐘能做多少事情嗎?

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

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

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

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

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

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

祝你好運(yùn)!

[[154392]]

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

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

 

  1. #include <stdlib.h> 
  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

 

準(zhǔn)確答案: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])) 

 

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

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

猜猜下面的程序每秒執(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. 準(zhǔn)確答案:11,000,000 

猜猜下面的程序每秒處理多少次HTTP請(qǐng)求:

 

  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. 準(zhǔn)確答案:25,000 

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

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

猜猜下面的程序每秒可以完成多少次HTTP請(qǐng)求:

 

  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])) 

準(zhǔn)確答案: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 

準(zhǔn)確答案:77

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

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

猜猜下面的程序每秒可以寫入多少字節(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])) 

準(zhǔn)確答案: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])) 

準(zhǔn)確答案:2,000,000,000

下面輪到文件了!有時(shí)候,運(yùn)行一個(gè)大型的grep之后,它可以永恒跑下去。在1秒時(shí)間內(nèi),grep可以搜索多少字節(jié)?
請(qǐng)注意,在這么做的時(shí)候,grep正在讀取的字節(jié)已經(jīng)在內(nèi)存中。
文件列表同樣需要時(shí)間!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. 準(zhǔn)確答案: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 

準(zhǔn)確答案:325,000

序列化是一個(gè)普遍要花費(fèi)大量時(shí)間的地方,讓人很蛋疼,特別是如果你反復(fù)結(jié)束序列化/反序列化相同數(shù)據(jù)的時(shí)候。這里有幾個(gè)基準(zhǔn):轉(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])) 

準(zhǔn)確答案: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])) 

準(zhǔn)確答案:4,000

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

猜猜下面的程序每秒可以執(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])) 

準(zhǔn)確答案: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])) 

準(zhǔn)確答案:2

下面要說Hash算法!在這里,我們將比較MD5和bcrypt。用MD5你在1秒時(shí)間內(nèi)可以哈希到相當(dāng)多的東西,而用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])) 

準(zhǔn)確答案: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])) 

準(zhǔn)確答案:3

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

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

 

  1. #include <stdlib.h> 
  2. #include <stdio.h> 
  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

準(zhǔn)確答案:376,000,000

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

 

  1. #include <stdlib.h> 
  2. #include <stdio.h> 
  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

準(zhǔn)確答案: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?

 

責(zé)任編輯:王雪燕 來源: 碼農(nóng)網(wǎng)
相關(guān)推薦

2023-06-01 08:22:13

2019-07-04 05:22:02

物聯(lián)網(wǎng)設(shè)備物聯(lián)網(wǎng)IOT

2018-05-07 15:32:54

編程語言Python程序員

2022-08-11 08:46:23

索引數(shù)據(jù)結(jié)構(gòu)

2024-06-04 16:51:11

2024-05-06 00:30:00

MVCC數(shù)據(jù)庫

2018-11-28 10:00:42

React組件前端

2023-05-04 12:43:26

機(jī)器學(xué)習(xí)算法

2017-08-23 18:31:28

華為

2023-01-31 09:02:24

JSVMVR

2018-01-02 09:31:12

大數(shù)據(jù)數(shù)據(jù)互聯(lián)網(wǎng)

2019-05-08 16:00:48

人工智能人臉識(shí)別刷臉

2023-08-28 07:39:49

線程調(diào)度基本單位

2021-07-26 23:57:48

Vuex模塊項(xiàng)目

2024-02-05 11:55:41

Next.js開發(fā)URL

2019-07-29 09:20:30

5G基站功耗運(yùn)營商

2024-08-08 09:15:08

SQL代碼復(fù)制表

2024-03-08 13:33:08

PG數(shù)據(jù)安全

2023-08-02 08:14:33

監(jiān)控MTS性能

2022-03-23 15:36:13

數(shù)字化轉(zhuǎn)型數(shù)據(jù)治理企業(yè)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)