SICP 1.23-1.26体会
1.23 代码修改非常easy, 关键是时间。 电脑上算了一下。 100000000下面全是0。 開始还以为代码写错了。
最后没办法, 用1e10 1e11来计算。 发现比 1e11 1e12快1.2-1.5之间。
比2小。
想了半天也给不出非常合理的解析。
開始以为是对3 5 7 取余数 比 4 6 8 要慢, 測试了一下,发现也不是这么一回事。网上有人怀疑是函数调用花了一定时间做if 推断, 老实说这东西性能影响也不应有这么大。
如今唯一想到的,就是编译器做了一些优化,导致性能不全然取决于调用次数。
这题目。网上也没找到非常合理的分析。
1.24 也非常遗憾。 開始将times设为10。 计算时间1e10下面全然是0. 后面没办法,将times设为10000, 结果发现时间也非常漂浮不定。
有时候, 1e12-1e13 比 1e11-1e12计算速度还快。
仅仅能说random导致性能非常随机了。
1.25 理论上肯定是对的, 时间上就悲剧了。我这边运行 100000000 1000000000区间,直接死机。
根本原因,还是我们的CPU是32或64位的。 不是无限位。
1.26 这个题目算法导论有清晰的解析。套用算法导论的公式T(n) = 2T(n/2) 可知道 T(n) = Theta(n)
坦率地说, SICP不是学算法的好书。 也不是讲数据结构的好书。
个人代码例如以下:
1.23
(define (search-for-primes-new start end count)
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (prime? n)
(report-prime (- (runtime) start-time))
0))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time)
1)
(define (prime? n)
(= n (smallest-divisor-new n)))
(define (smallest-divisor-new n)
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (divides?
a b)
(= (remainder b a) 0))
(define (next test-divisor)
(if (= test-divisor 2)
3
(+ test-divisor 2)))
(find-divisor n 2))
(define (search-iter start end count)
(if (or (> start end) (= count 0))
0
(if (= (timed-prime-test start) 1)
(search-iter (+ start 1) end (- count 1))
(search-iter (+ start 1) end count))))
(search-iter start end count))
1.24
(define (search-for-primes-new start end count)
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (fast-prime? n 10000)
(report-prime (- (runtime) start-time))
0))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time)
1)
(define (search-iter start end count)
(if (or (> start end) (= count 0))
0
(if (= (timed-prime-test start) 1)
(search-iter (+ start 1) end (- count 1))
(search-iter (+ start 1) end count))))
(search-iter start end count))
(define (expmod base exp m)
(cond ((= exp 0) 1)
((even? exp)
(remainder (square (expmod base (/ exp 2) m))
m))
(else
(remainder (* base (expmod base (- exp 1) m))
m))))
(define (fast-prime?
n times)
(cond ((= times 0) true)
((fermat-test n) (fast-prime?
n (- times 1)))
(else false)))
(define (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
1.25
(define (search-for-primes-25 start end count)
(define (timed-prime-test n)
(newline)
(display n)
(start-prime-test n (runtime)))
(define (start-prime-test n start-time)
(if (fast-prime? n 10000)
(report-prime (- (runtime) start-time))
0))
(define (report-prime elapsed-time)
(display " *** ")
(display elapsed-time)
1)
(define (search-iter start end count)
(if (or (> start end) (= count 0))
0
(if (= (timed-prime-test start) 1)
(search-iter (+ start 1) end (- count 1))
(search-iter (+ start 1) end count))))
(search-iter start end count))
(define (expmod base exp m)
(remainder (fast-expt base exp) m))
(define (fast-expt base exp)
(cond ((= exp 0) 1)
((even?
exp)
(square (fast-expt base (/ exp 2))))
(else
(* base (fast-expt base (- exp 1))))))
(define (fast-prime? n times)
(cond ((= times 0) true)
((fermat-test n) (fast-prime?
n (- times 1)))
(else false)))
(define (fermat-test n)
(define (try-it a)
(= (expmod a n n) a))
(try-it (+ 1 (random (- n 1)))))
SICP 1.23-1.26体会的更多相关文章
- Part 23 to 26 Routing in Angular
Part 23 AngularJS routing tutorial In general, as the application becomes complex you will have more ...
- ZJOI2019Day2余姚中学游记(4.23~4.26)
前言 \(Day2\),又是一场噩梦. 前段时间去做了挺多十二省联考和\(HNOI2019\)的题目,还订正掉了\(Day1\)的\(T1\)和\(T2\)(\(T3\)动态\(DP\)完全不想订正啊 ...
- SICP 1.21 1.22 体会
1.21 简单的将书上代码敲了一遍. 非常顺利就过了. 1.22 就悲剧了. 先按书本的意思.代码非常快就写完了.但计算的时间在机子上漂浮不定. 3-5倍之间. 代码例如以下: (define (se ...
- SICP 习题1.16-1.19体会
首先反思一下, 昨天做1.14的时候犯了一个严重错误.思维定式了,导致花了非常多无用功. 1.14的关键是要想到2个物理意义. 一个是广度优先, 也就是仅仅考虑问题递归树的第一层子数.那么必定有公式 ...
- 26. 60s快速定位服务器性能问题
60s迅速发现性能问题 uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat -xz 1 free -m sar -n DEV 1 ...
- 最佳vim技巧
最佳vim技巧----------------------------------------# 信息来源----------------------------------------www.vim ...
- 【1414软工助教】团队作业4——第一次项目冲刺(Alpha版本) 得分榜
题目 团队作业4--第一次项目冲刺(Alpha版本) 作业提交情况情况 所有团队都在规定时间内完成了七次冲刺. 往期成绩 个人作业1:四则运算控制台 结对项目1:GUI 个人作业2:案例分析 结对项目 ...
- 斐波那契数列的三种C++实现及时间复杂度分析
本文介绍了斐波那契数列的三种C++实现并详细地分析了时间复杂度. 斐波那契数列定义:F(1)=1, F(2)=1, F(n)=F(n-1) + F(n-2) (n>2) 如何计算斐波那契数 F( ...
- Alpha冲刺(10/10)——追光的人
1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...
随机推荐
- ThreadPoolExecutor(线程池)源码分析
1. 常量和变量 private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); // 高3位为线程池的运行状态,低29 ...
- es进行聚合操作时提示Fielddata is disabled on text fields by default
在进行派粗前,先执行以下操作 { "properties": { "updatedate": { "type": "text&qu ...
- iOS \U6b3e转字符串
-(NSString *)replaceUnicode:(NSString *)unicodeStr { NSString *tempStr1 = [unicodeStr stringByReplac ...
- maven-忽略文件-.gitignore文件
# kdiff3 ignore *.orig # maven ignore target/ # eclipse ignore .settings/ .project .classpath # idea ...
- 发一个比trace功能更强大debug工具,MonterDebugger
经常看到兄弟说trace不出东西啊,这样给你调试会带来很多不便:加入说我们需要将运行时的debug信息和之前某个版本的进行比对:又加入说我们需要在运行时通过debug动态调整显示对象的属性:查看当前整 ...
- Postgres间隔大量写IO的解决办法
概述 为了保证数据可靠性,同时还要保证好的读写性能,以及读写的一致性,经过多年的积累,REDO日志,shared buffer等基本成为关系型数据库的标配.postgres也不例外. 为了保证数据的可 ...
- [转]Sql server 大数据量分页存储过程效率测试附代码
本文转自:http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html 在项目中,我们经常遇到或用到分页,那么在大数据量(百万级以上)下 ...
- How to publish a WordPress blog to a static GitLab Pages site
https://opensource.com/article/18/8/publish-wordpress-static-gitlab-pages-site A long time ago, I se ...
- 解决SSH窗口关闭,linux上的应用也关闭
最近在应用linux上的服务的时候发现一个问题 使用SSH远程连接启动的应用在SSH关闭的时候也死掉了,网上查了一下原因 大致是说SSH在关闭的时候会发送一个终止的指令给应用,然后就停了 简要的解决办 ...
- mavn项目(springMVC) 引入静态资源(js、css)等
在web.xml中配置 <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern ...