Archive for the ‘Programming’ Category.
April 2, 2010, 12:34 am

目前全球有约10亿台计算机,每台按100Wh实际功率计算,总功率为1亿千瓦时。也就是说每小时耗电量为1亿千瓦。按计算机一天工作10小时计算,一天全球消耗总电量为10亿千瓦时。世界超大型水电站(如三峡电站)在汛期全部机组开动发电量不过4亿千瓦时(参见)。意味着我们需要2-3个这样的超大水电站全负荷供电才能满足基本需求。我们知道,建设一个这样的电站要耗费数以亿计的金钱和数十年的人工。但是,如果作为程序员,往往可以轻易地提高编码质量和运行效率以达到节省计算时间的目的,变相地节约了宝贵的能源,有利于保护地球环境。我觉得如果所有程序员都朝这个方向努力,每天节省一个三峡电站的电量是完全可能的。
April 1, 2010, 5:14 pm
Bash下可以使用”cd -”来实现在当前目录和上一次访问的路径间切换。 如果我们需要回退多级目录怎么实现呢?
Bash有两个内置命令pushd和popd,每次调用它们,系统会把当前目录压入目录栈中保存起来,或者从栈顶弹出一级目录并进入那个目录。利用这两个命令可以实现访问路径的存储与调用。为了使用方便可以在~/.bashrc中创建一个自己的命令,例如
1 2 3 4 5 6
| function cd {
if [ -n "$1" ]; then pushd $1 > /dev/null
else pushd $HOME > /dev/null
fi
}
alias pd='popd > /dev/null' |
这里创建的cd函数覆盖了built-in的cd命令,可以自动保存当前路径到栈中。而调用pd命令则可以返回上次访问的目录。
如果我们想恢复系统默认的cd命令怎么办呢?可以使用“unset -f cd”来取消自定义的cd函数。
Continue reading ‘Bash下的目录回退’ »
March 22, 2010, 7:54 pm
By default Notepad2 add a .txt extension to the file name when you save a newly created file. For files already having extensions, this will not be done. However, this is sometimes annoying if you often create files which do not use txt as extension although they are actually plain text files. I think this is almost a daily work for programmers. To cancel this feature, there are two ways. First, when you save the newly created file, simply add quote symbols around the file name like “new_file”, then the file will be save without an extension or with any extension you give. This trick also works for the Windows Notepad program. Another hack is to modify notepad2.ini, there you can create a new section (if it’s not there already) named “[settings2]“. The settings given in this section will override the default behavior. Then add an entry “DefaultExtension=”, yeah just leave nothing behind the equal sign, no suffix will be appended automatically then. There are more options for the customized section, see here.
No doubt, Notepad2 should be a must-have replacement on Windows.
March 22, 2010, 12:58 pm
经常在C++程序中加入中文注释,但是如果使用不同的编辑器,编码识别或转换不当,会导致非ASCII字符乱码。
考虑到目前绝大多数编辑器都支持unicode编码(UTF-8)的源文件,我觉得一个稳妥的方式是把自己的代码都保存为unicode(UTF-8)。把原来的ACSII编码的文件转换为unicode网上有一些批量转换工具。值得一提的是众多文本编辑器中只发现EditPlus可以做批量转换,其它编辑器如Notepad++都只能一个文件一个文件地转换(知道如何批量操作的朋友请给些提示?)。VS也可以选择保存文件时的编码格式(在高级保存选项中设置)。还有一个要注意的地方是unicode≠UTF-8,在这点上我以前没有注意,(这里的一篇博文说清楚了这两种编码间的异同,主要区别是unicode固定使用四字节编码所有语言,而UTF-8是可变长度的编码,所以并不一样。)
通过试验得知,VC++和Intel的编译器均支持直接编译带有BOM的UTF-8源文件,但是gcc却不支持。事实上gcc和iconv都不支持读取BOM,但是对于没有BOM的UTF-8编码文件却可以编译。因此,对于跨平台的程序,如果想同时使用VC++和gcc来编译有BOM或无BOM的UTF-8源文件都存在兼容问题。(关于BOM (Byte order mark),wikipedia上有所讨论。)
Continue reading ‘用VC和gcc同时编译unicode编码的c++源程序’ »
March 11, 2010, 11:57 pm
某些VS的Addins会导致程序异常缓慢或出错,一个小技巧是在启动VS的时候提前按住左边的shift键,即可禁止加载所有插件。
March 1, 2010, 4:32 pm
Both Linux Bash and Windows DOS support random number generation. The random number is ‘stored’ in a system environment variable named “RANDOM” (I believe this variable links to a script to generate the actual random numbers on both systems), use it in the same way as you use other ENV variables. E.g.
On Linux:
;
Continue reading ‘Get random numbers under BASH and DOS’ »
February 10, 2010, 1:24 pm
February 5, 2010, 1:54 pm
Useful commands:
- wait <pid>: wait the background job with specified pid to be finished and return the exit code ( hint: stored in $? )
- jobs -p: list the pids of all the background jobs (hint: use for statement of bash to loop over each process)
- ( sleep 30s; /bin/false ) & : generate a background testing job with a error exit code
First, submit all the background jobs, no matter you use loop or line by line in a script.
Next, get the PIDs of the background jobs. After submitting EACH job, the PID of that job is in $!, which can be store to any user defined variable for later use.
Last, check the exit codes. You can either check the exit code of each job one by one or calculate the accumulative exit code. To check them one by one, just call wait command for each PID stored in previous step and check the value of $?; to check them cumulatively, see this post. (the trick is the sum of exit codes for all jobs should be zero if all jobs are finished successfully, or the sum must be non-zero)
January 12, 2010, 2:42 pm
This is just a rough comparison of the influence of compilers on the performance of lammps code on a VIC3.
OpenMPI 1.4 is installed and used during the compilation. A short REMD job using 26 CPUs was tested. (I think I don’t need to attach the job files here, because this comparison as you will see is really rough. The idea is just to get some brief knowledge of the performance influenced by the different compilers. It’s absolutely NOT scientific.)
Continue reading ‘Lammps performance with different compilers’ »
January 8, 2010, 12:42 pm
It’s always nice to get notified about the status of a long job, e.g. copying a huge file via a not-that-fast network. For GUI programs, normally you get a progress bar, but we have no such luxury under bash. But wait, here is a nice bash script can do that. It works pretty well. You will find it useful in many places.
http://www.theiling.de/projects/bar.html