code kata NO.3 - Kata Three: How Big, How Fast?

原创 2011年01月10日 22:34:00

kata3的主题是 how big ,how fast,以下提供的解答参考自回复者szeryf,我基本是拾人牙慧+翻译,呵呵

How big

Q1.roughly how many binary digits (bit) are required for the unsigned representation of:



A1. 2^10=1024,所以1000需要10个bit,1000000是1000*1000,对于2进制数,乘法就是向左移动多少位的问题,


      同理 1,000,000,000需要30位 ,1,000,000,000,000需要40位,8,000,000,000,000是乘以2^3=8,因此是40+3 = 43位


      验证,使用计算机计算2^43 = 8796093022208 ,2^42 = 4398046511104 ,8,000,000,000,000应该是43位


Q2.My town has approximately 20,000 residences. How much space is required to store the names, addresses, and a phone number for all of these (if we store them as characters)




Q3. I’m storing 1,000,000 integers in a binary tree. Roughly how many nodes and levels can I expect the tree to have? Roughly how much space will it occupy on a 32-bit architecture?




存储的话,数据,左指针,右指针,各占32bit,也就是96bit,就是12byte,也就是 12000000 byte


存储的话是2倍也就是24000000 byte




How Fast

Q1. My copy of Meyer’s Object Oriented Software Construction has about 1,200 body pages. Assuming no flow control or protocol overhead, about how long would it take to send it over an async 56k baud modem line?


A1. 1200page * 每页多少行 * 每页多少字 得出大概的字,然后根据字的编码占用的空间大小得出bit总数,比如ASCII就是*8

然后总数除以 56000 bit  即可得到秒数


Q2. My binary search algorithm takes about 4.5mS to search a 10,000 entry array, and about 6mS to search 100,000 elements. How long would I expect it to take to search 10,000,000 elements (assuming I have sufficient memory to prevent paging).


A2. 10000 最糟情况需要二分14次,100,000 最糟情况需要二分17次

       则 4.5ms/14 = 每次需 0.32ms ,6ms/17 = 每次需要 0.35ms

       10,000,000  最糟情况需要二分24次 ,24次*0.35ms = 8.4 ms


Q3. Unix passwords are stored using a one-way hash function: the original string is converted to the ‘encrypted’ password string, which cannot be converted back to the original string. One way to attack the password file is to generate all possible cleartext passwords, applying the password hash to each in turn and checking to see if the result matches the password you’re trying to crack. If the hashes match, then the string you used to generate the hash is the original password (or at least, it’s as good as the original password as far as logging in is concerned). In our particular system, passwords can be up to 16 characters long, and there are 96 possible characters at each position. If it takes 1mS to generate the password hash, is this a viable approach to attacking a password?


A3. (题很长实际很简单) 16位长的密码,每位有96种可能,则总变化数是 16^96 = 2^384 

     每次尝试使用1ms,则2^384*1ms =  2^384ms , 这个数字换算成年,大概地球毁灭了 :)



code Kata NO.1

Kata Two -- Karate Chop A binary chop (sometimes called the more prosaic binary search) finds t...

Code Kata/dojo-handbook

  • 2013-12-27 22:40
  • 1.43MB
  • 下载

终极编程道场(The Ultimate Code Kata)

今天在Coding Horror上看到一篇不错的文章,原文链接:         或许与你相信...

How to write fast numerical code

  • 2015-04-08 13:56
  • 557KB
  • 下载

18-645:How to Write Fast Code 期中考试准备

Background multicore programming manycore programming18-645:how to write fast code快要期中考试了,所以这里对过去所学的...

How to write fast numerical code

  • 2014-03-13 15:59
  • 557KB
  • 下载

How to write the fast code/ high performance in C#

Performance Techniques for C# 4.0 Anthony Moore Contents Contents.1 Overview..1 Patterns by C...

CC150 3.1 Three in one: describe how you could use a single array to implement 3 stacks.

fixed Division思路: 1 把数组等分; 2 允许stack在其中增长; stack0 [0,n/3) stack1 [n/3,2n/3) stack2 [2n/3,...