由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 一道有趣的面试题。stackexchange上超火 大家说说看法?
相关主题
问一道C++ template的面试题昨天的google面试题
问一个linkedin的面试题[合集] 昨天的google面试题
c++ is too nasty贴两个比较tricky,又常被问到的面试题
两道面试题,请大家说说看法一道有意思的Google面试题
Linux context switch 高通 面试题。??请教个面试题
问道National Instruments面试题求助一个面试题
[合集] google 面试题请问这段代码什么意思?
贴一道take home的面试题写了个sqrt,请点评
相关话题的讨论汇总
话题: case话题: reasons话题: iterator话题: faster话题: code
进入JobHunting版参与讨论
1 (共1页)
H******7
发帖数: 1728
1
http://programmers.stackexchange.com/questions/64132/interestin
投票最高的答案说的很简略 有人给解释解释不?
Today I was asked this question. We have 2 cases with code blocks A, B and C. These code block don't share
any resources except an iterator (int i).
Please give 3 possible reasons why case 1 could be faster than case 2, and 3 possible reasons why case 2
could be faster than case 1:
case 1
for (i=0; i A;
B;
C;
}
case 2
for (i=0; i A;
}
for (i=0; i B;
}
for (i=0; i C;
}
l*****g
发帖数: 685
2
这个跟iterator(int i)怎么implement有关吧
btw, iterator(int i)这样的API应该说是个Indexer, 而不是iterator. iterator是一
个一个按顺序取元素,Indexer可以取任意指定的位置

C. These code block don't share
3 possible reasons why case 2

【在 H******7 的大作中提到】
: http://programmers.stackexchange.com/questions/64132/interestin
: 投票最高的答案说的很简略 有人给解释解释不?
: Today I was asked this question. We have 2 cases with code blocks A, B and C. These code block don't share
: any resources except an iterator (int i).
: Please give 3 possible reasons why case 1 could be faster than case 2, and 3 possible reasons why case 2
: could be faster than case 1:
: case 1
: for (i=0; i: A;
: B;

g*******s
发帖数: 490
3
我觉得那个答案解释挺清楚的
reasons for case 1 faster than case2
(1)执行loop的每次循环本身有有cost,即时你loop里什么code也没有。case 1是N次循
环,case 2是3N次循环
(2)方便并行计算
(3)A,B,C依次运行,他们虽然不share resources,但是他们的code某些部分可以被共用
,例如:假设每一次循环中,A,B,C都需要读取同一个input,这部分的代码就可以共用,
然后即时A,B,C用来存储这些input的object不同,但是类似i/o buffer之类的都可以
被重用
reasons for case 2 faster than case 1
(1) 寄存器,在case 1中,每一次循环要执行3个code block,这些代码段的load都是
有cost的,等于每一次循环有3次的transition,这样就是3N次,而case 2等于只有2次
,因为一段代码连续执行N个循环,就没有这个反复load,包括寄存器参数恢复的cost
(2)http://en.wikipedia.org/wiki/Loop_unwinding,便于编译器做更底层的code optimization
(3)和(1)差不多了,假使code比较短,可以整个装进cache,自然运行速度就快乐
1 (共1页)
进入JobHunting版参与讨论
相关主题
写了个sqrt,请点评Linux context switch 高通 面试题。??
问一道微软面试题问道National Instruments面试题
请问一道面试题Iterator over nested collections[合集] google 面试题
请教一个新鲜算法面试题贴一道take home的面试题
问一道C++ template的面试题昨天的google面试题
问一个linkedin的面试题[合集] 昨天的google面试题
c++ is too nasty贴两个比较tricky,又常被问到的面试题
两道面试题,请大家说说看法一道有意思的Google面试题
相关话题的讨论汇总
话题: case话题: reasons话题: iterator话题: faster话题: code