2012年3月19日星期一

inline and macro difference

First let's talk about the difference between macro and function

1. macro is done before compile, that's also why it is called pre-compile. So first of all, we use macro body to replace macro name. Then we do compile.

On the other hand, function is executed after compile. Actually, it is called in the process of execution. So we can see that macro take compile time and function take execution time.

2. Because we need to "call" function, we need to pay for some resource(cost). We need to keep the state, and then go into the called function, after "call" finish, we need to go back the function which call others. Then we need to recover state.

On the other hand, all these operation above will never happen in macro. What macro do is just string replacement(string! not other type). So the argument of macro will not occupy memory space. But function argument need memory space, since argument is variable info transfer. Argument is function's local variable. Also, we do not need to do computation for macro argument. Function is give the value of argument. Here, we use value which will lead to computation.

3. As we said above, macro is string replacement. But argument of function can be any type.
-----------------

What inline do is to "embed" its code to the function which call it. So we can save cost to call it.
We need be careful about inline function must be very simple. No loop, if else, etc.

PS: some compiler will handle simple function as inline automatically. Some complex function, even you declare it is inline, compile will not do so.
---------

NO grammar examination for MACRO! Inline will have grammar exam during compiling. That is the reason why most programmer choose inline but not macro.

2012年3月17日星期六

跟着环境走 放下身段

今天看一个视频,有两段话记下:
-------------------

一段是说一个女老板在日本开牛肉面店:

人要跟着环境走,跟着环境走不是说要顺流而下,而是你要跟周围的环境取得一个和谐的关系,你做什么就要像什么。

一段是一个师傅教咏春评价徒弟:

他学拳有少许障碍,就是爱面子。学拳有时,不管学拳其他东西也一样。如果不放下身段,很难有长足的进步。你不愿服输,如何能赢?
-----------------------
想到自己最近总是怀疑自己的能力,最终还是会调试出来。既然是自己选的路,不管自己的先天条件有多少,都不应该怀疑自己,如果连自己都不相信,遇到困难就会很沮丧而退缩。

而学东西的时候总是觉得自己工作过,尽管心底知道这和现在学的关系不大,但遇到问题总是不好意思去及时问别人,失去好多学习的机会。积少成多,自然不会出色。

熟能生巧,触类旁通,这能达到这个境界是要付出时间和精力的。另外,就是要学会集中注意力,这点也很重要,注意力意味着效率。