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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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,自然运行速度就快乐 |
|