This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / c/c++ interview question, thanks.Imagine that you're trying to optimize a critical section of code that is executed extremely frequently. In your inspection of the code, you find this:
if ((a % 128) == 0)
Can this be optimized? If so, how?
-neu_stone(stone);
2011-5-13
{211}
(#6674580@0)
-
bit operation
-c1xwy(洪兴罩俺去战斗);
2011-5-13
(#6674585@0)
-
?
-neu_stone(stone);
2011-5-13
(#6674934@0)
-
bit operation (#6648657@0)
-tjhong(以后再说);
2011-5-13
(#6675042@0)
-
if (a & 127 == 0)
-c1xwy(洪兴罩俺去战斗);
2011-5-13
(#6675051@0)
-
a%128 太简单了, 编译器自己就能优化
-upad(铁岭屯二道街);
2011-5-16
(#6680333@0)
-
可以把 if ((a % 128) == 0) 优化为 if( (a & 127) == 0 )。((a & 127) 的括号不能少。)由于a的值可能为负,所以这个结果需要说明一下。
-perryuan(perryuan);
2011-5-18
(#6685810@0)
-
我把上述问题一般化,改为如何优化
-perryuan(perryuan);
2011-5-18
(#6685840@0)
-
1、unsigned uint; 优化表达式 uint % 128
-perryuan(perryuan);
2011-5-18
(#6685841@0)
-
2、int sint; 优化表达式 sint % 128
-perryuan(perryuan);
2011-5-18
(#6685842@0)
-
3、unsigned uint; 优化表达式 uint / 128
-perryuan(perryuan);
2011-5-18
(#6685843@0)
-
4、int sint; 优化表达式 sint / 128
-perryuan(perryuan);
2011-5-18
(#6685845@0)
-
这个属于脑残问题啊。几乎所有的编译器这个都自动优化。如果不是问优化,而是multi-threading保护问题,倒还有些道理。你确认你明白了原来的问题?
-steve888(Steve曾经曰过);
2011-5-18
(#6685864@0)
-
I didn't change any word of this question. Volatile should be considered ?
-neu_stone(stone);
2011-5-19
(#6689008@0)
-
那倒也不一定。自动优化是一个编译开关,有可能是关闭的。在有些芯片上,尤其是实时系统的芯片上,靠编译器自动优化有时效果没有人工优化效率高。
-eggplant(老茄子);
2011-5-19
(#6690721@0)
-
觉得你们都overqualified,这个问题还是挺适合当入门级的面试题目吧。
-c1xwy(洪兴罩俺去战斗);
2011-5-19
(#6690860@0)
-
c/c++ interview question (2)) Identify some of the features of the C++ language and standard libraries that may not be suitable for a relatively slow computer (e.g., 400MHzCPU, 32mb RAM). For each feature identified, explain why the feature is not suitable, and what a reasonable alternative may be.
-neu_stone(stone);
2011-5-19
{271}
(#6689004@0)
-
题目很大, 可以向如何提高C++ 程序的 Performance 来回答。先扔块砖。针对Runtime Performance,尽量少用 virtual 的 method 和 inheritance,少用 exception,少用类库等之类;针对 Compilation Performance,尽量少用 template,这东西用多了对一个稍微大点的程序来说,光编译一次就得8 -9个小时,BOOST 类库就是个例子;还有如果内存很小,而执行代码大肯定是不好使的。
-kaia(顺流逆流);
2011-5-19
{282}
(#6689796@0)
-
这都哪找的面试题啊?出这种题的人,我肯定不会录用。C++问世的时候,400MHzCPU, 32mb RAM的配置,算得上超级配置了。
-majorhomedepot(马甲后的炮);
2011-5-19
(#6690668@0)
-
传参数时不要传 object, 而要用指针,因为整个Object 会被压栈. String 类不是很合适,它是mutable class. 做参数时会被copy 并压栈. 这是我能想到的,不一定对。
-eggplant(老茄子);
2011-5-19
(#6690762@0)
-
C++要多用引用,少用指针。
-majorhomedepot(马甲后的炮);
2011-5-20
(#6692922@0)
-
这是对中低级Programmer 而言. 引用的长处在于可以利用编译器的类型 Validation. 减少错误。但有些操作引用是无法做的。从Performance 角度,引用和指针是完全一样的。
-eggplant(老茄子);
2011-5-29
(#6709096@0)
-
那传参数的时候, 你通常是用指针还是引用啊?中级可能都算不上,Basic的要求啊。
-majorhomedepot(马甲后的炮);
2011-5-29
(#6709249@0)
-
I always choose pointer if I can. I agree it is more dangerous than reference, but by the same reason it gives you a chance to keep the type and memory allocation in your mind all the time. It is important because you cannot avoid pointer in C++
-eggplant(老茄子);
2011-5-30
(#6709600@0)
-
你给解释真逗: 1. To keep the type and memory allocation in your mind all the time; 2. you cannot avoid pointer.
-majorhomedepot(马甲后的炮);
2011-5-30
(#6709691@0)