惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
月光博客
月光博客
Recent Announcements
Recent Announcements
S
Secure Thoughts
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Troy Hunt's Blog
量子位
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
云风的 BLOG
云风的 BLOG
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
WordPress大学
WordPress大学
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
P
Privacy & Cybersecurity Law Blog
aimingoo的专栏
aimingoo的专栏
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V2EX - 技术
V2EX - 技术
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
Darknet – Hacking Tools, Hacker News & Cyber Security
U
Unit 42
Schneier on Security
Schneier on Security
大猫的无限游戏
大猫的无限游戏
I
Intezer
Hacker News: Ask HN
Hacker News: Ask HN
H
Heimdal Security Blog
Cisco Talos Blog
Cisco Talos Blog
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Help Net Security
Latest news
Latest news
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LangChain Blog
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
小众软件
小众软件
M
MIT News - Artificial intelligence
A
About on SuperTechFans
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
NISL@THU
NISL@THU
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
Lohrmann on Cybersecurity
T
Tenable Blog

Puzzle | 酷 壳 - CoolShell

谜题的答案和活动的心得体会 | 酷 壳 - CoolShell 【活动】解迷题送礼物 | 酷 壳 - CoolShell 一个fork的面试题 | 酷 壳 - CoolShell 面试题:火车运煤问题 | 酷 壳 - CoolShell “火柴棍式”程序员面试题 | 酷 壳 - CoolShell 打印质数的各种算法 | 酷 壳 - CoolShell 输出从1到1000的数 | 酷 壳 - CoolShell 140个Google的面试题 | 酷 壳 - CoolShell 面试题:布尔变量 | 酷 壳 - CoolShell 面试题:赛马问题 | 酷 壳 - CoolShell
又一个有趣的面试题 | 酷 壳 - CoolShell
陈皓 · 2011-04-02 · via Puzzle | 酷 壳 - CoolShell

大家还记得前些天的那个火柴棍式的面试题吗?很有趣吧。下面是我今天在StackExchange上看到的一个有趣的面试题。大家不妨一起来思考一下。问题如下——

有两个相同功能代码如下,请在在A,B,C是什么的情况下,请给出三个原因case 1比case 2快,还有三个原因case 2会比case 1要执行的快。(不考虑编译器优化)

for (i=0; i<N; ++i){
    A;
    B;
    C;
}
for (i=0; i<N; ++i){
    A;
}
for (i=0; i<N; ++i){
    B;
}
for (i=0; i<N; ++i){
    C;
}

我的第一个反应是——

  • case1 要快一些,因为只有一个i++的i<N的操作,而case 2却有三个,这在点上,case 1就比case 2要快。
  • case2如果要快的话,有一个原因是,A, B, C其中一个需要去先获得一个资源(比如一个锁),在case1下,每次都要去拿这个资源,而case2下,只需要拿一次然后。但这个可能是不对的,因为我无法想出一个相同的语句块放在case 1中会和放在case 2中有差别。(不过可能比较接近了)

继续思考:这个题有点像是“同步和异步”的问题,case 1是同步,case 2是异步,所以,异步快于同步,也许可以从这个方向出发,写出A, B, C的语句块。

不过,其要三个原因啊。各位,你们有想法吗

—-更新 1—-

刚才在twitter上与人讨论,发现又有一种情况,case 2要比case 1要快。比如,A, B, C分别访问是不同的内存块(数组),那么case 1就得在不同的内存块上来回切换寻址,而case2则可以连续地访问内存块。访问连续的内存效率要高。尤其是三块大内存。

—-更新 2—

正如本贴评论中所说的,CPU的cache也是其中一个因素。大家对底层知识了解的都很不错啊。赞一个。

Loading...