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

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

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...