This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Latency Comparison Numbers | |
-------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns | |
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms | |
Read 4K randomly from SSD* 150,000 ns 0.15 ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class memoized(object): | |
'''Decorator. Caches a function's return value each time it is called. | |
If called later with the same arguments, the cached value is returned | |
(not reevaluated). | |
''' | |
def __init__(self, func): | |
self.func = func | |
self.cache = {} | |
def __call__(self, *args): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. 编写一个BankAccount类,加入deposit和withdraw方法,和一个只读的balance属性。 | |
class BankAccount(val balance: Double) { | |
def deposit(amount: Double): BankAccount = { | |
require(amount > 0) | |
new BankAccount(balance + amount) | |
} | |
def withdraw(amount: Double): BankAccount = { | |
require(amount > 0 && amount <= balance) |
NewerOlder