汉辰攻略

The palest ink is better than the best memory.

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  8 Posts :: 1 Stories :: 3 Comments :: 0 Trackbacks

2008年11月12日 #

  • C-j: Insert a new line with the same indentation level as the current line
  • RET: Insert a new line with the same indentation level as the current line
  • C-M-a: Go to the beginning of the current function or class
  • C-M-e: Go to the end of the current function or class
  • C-M-h: Mark the current function or class for copying, etc.
  • C-M-x: Execute the current function or class
  • C-c C-b: Submit a bug report
  • C-c C-c: Execute the buffer (i.e., the file being displayed)
  • C-c C-d: Trace the stack of the process being executed
  • C-c C-h: Get context-based help
  • C-c TAB: Indent a highlighted (or marked) region
  • C-c C-k: Mark a block of text. Using this at the head of a class or function definition will mark the entire block.
  • C-c C-l: Shift the region to the left. If the cursor is in the middle of a region, the lower half of the region will shift.
  • C-c RET: Execute the current file, opening a new window to show the output.
  • C-c C-n: Jump to the next statement.
  • C-c C-p: Jump to the previous statement.
  • C-c C-r: Shift the region to the right. If the cursor is in the middle of a region, the lower half of the region will shift.
  • C-c C-s: Execute a Python command.
  • C-c C-t: Toggle shells
  • C-c C-u: Go up one block
  • C-c C-v: List the version of the Python mode
  • C-c C-w: Run PyChecker
  • C-c !: Open the Python interactive shell
  • C-c #: Comment the highlighted (marked) region
  • C-c :: Check the indentation off-set
  • C-c <: Shift the region to the left
  • C-c >: Shift the region to the right
  • C-c ?: Show Python mode documentation
  • C-c |: Execute the highlighted (marked) part of the current program.
  • posted @ 2008-11-12 14:11 汉辰 阅读(1053) | 评论 (0)编辑 收藏

    2008年10月30日 #

    --XX:+DoEscapeAnalysis, off by default
    如果一个对象的所有引用都是限于某一局部范围,去掉对象上的锁
    --XX:+UseBiasedLocking, on by default
    对多次取锁的操作,比如循环,不释放Lease即使锁已被释放,避免费时的取Lease操作 (最有效)
    --XX:+EliminateLocks, on by default
    如果一段代码经常性的加锁和解锁,在解锁与下次加锁之间又没干什么事情,则可以将多次加加锁解锁操作合并成一对。也称为Lock Coarsening (锁粗化)

    http://www.infoq.com/articles/java-threading-optimizations-p1
    posted @ 2008-10-30 15:33 汉辰 阅读(657) | 评论 (0)编辑 收藏

    2008年9月9日 #

    python 有str object 和 unicode object 两种字符串, 都可以存放字符的字节编码,但是他们是不同的type,这一点很重要,也是为什么会有encode 和decode。

    encode 和 decode在pyhton 中的意义可表示为

                                                                      encode
                                                  unicode -------------------------> str
                                                  unicode <--------------------------str
                                                                      decode
    几种常用法:
    str_string.decode('codec') 是把str_string转换为unicode_string, codec是源str_string的编码方式
    unicode_string.encode('codec') 是把unicode_string 转换为str_string,codec是目标str_string的编码方式
    str_string.decode('from_codec').encode('to_codec') 可实现不同编码的str_string之间的转换
    比如:

    >>> t='长城'
    >>> t
    '\xb3\xa4\xb3\xc7'
    >>> t.decode('gb2312').encode('utf-8')
    '\xe9\x95\xbf\xe5\x9f\x8e'

    str_string.encode('codec') 是先调用系统的缺省codec去把str_string转换为unicode_string,然后用encode的参数codec去转换为最终的str_string. 相当于str_string.decode('sys_codec').encode('codec')。

    unicode_string.decode('codec') 基本没有意义,unicode 在python里只用一种unicode编码,UTF16或者UTF32(编译python时就已经确定),没有编码转换的需要。 

    注:缺省codec在site-packages下的sitecustomize.py文件中指定,比如

    import sys
    sys.setdefaultencoding(
    'utf-8')



     

    posted @ 2008-09-09 15:49 汉辰 阅读(13124) | 评论 (1)编辑 收藏

    2008年7月28日 #

    慕名Emacs是很久的事了,上个周末Google了一天终于把它在Windows下的中文化问题初步解决了。

    网上讲述Emacs中文问题的帖子很多,不过很多是过时的或不完整的,并以Linux下居多。很多.emacs设置看起来发生了作用,但是在我的机器上不是显示方块,就是半个中文字符。前者怀疑未配置好Emacs使其无法读取系统字体,后者怀疑字符宽度设置还有问题,苦于无暇深究。

    最后找到一篇Emacs 中文化指南[http://17xie.com/read-103809.html],讲述了在Windows下使用了X11的BDF字体配置Emacs的过程,下载安装了intlfont,改了相应的emacs设置,Bingo!!!,一试就通。不过字库还是GB2312的,想把Mule-GBK 和BDF 字体配置在一起,未成功。

    贴张图庆贺一下
    posted @ 2008-07-28 13:47 汉辰 阅读(919) | 评论 (0)编辑 收藏

    2008年7月11日 #

    EMacs常用命令集:
    C-x C-s        当前文件存盘
    C-x s        所有文件存盘,根据提示确认是否保存指定文件
    C-x C-b        列出缓存
    C-x C-f        查找文件,如果不存在则新建,如果已打开则切换为当前文件
    C-x 数字        关闭其它窗,只显示当前主窗体。也可以输入其它数打开多窗
    C-x o        切换到另一个打开的窗
    C-z        临时退出,在X中相当于最小化,在Console中可以用"fg"或者"%emacs"返回。
    C-x C-c        退出
    C-u 数字        前缀参数,常用于重复执行命令,如插入8个*符:C-u 8 *
    M-x recover 文件名<回车>         恢复文件到上次自动保存的状态,常用于系统崩溃时
    C-h m        当前模式的说明文档,每种模式都略有些许不同
    C-h i        常用shell命令的说明文档

    常用移动命令:
    C-v        向下翻页
    C-M-v        另一窗向下翻页
    M-v        向上翻页
    C-l        移动到光标位置
    M- <-        移到文首
    M- ->        移到文尾
    C-a        移到行首        M-a        移到句首
    C-e        移到行尾        M-e        移到句尾
    C-p        上移一行
    C-n        下移一行
    C-b        左移一位        M-b        左移一字
    C-f        右移一位        M-f        右移一字
    常用编辑命令:
    C-k        删除到当前行尾
    M-k        删除到当前句尾
    C-g        中断执行
    C-w       Cut marked region
    A-w       Copy marked region
    C-y        取回所有删除的行,默认显示最后删除的行
    M-y        C-y取回所有删除的行之后,切换显示之前删除的行
    C-x C-x  Mark the region from the current cursor to last mark point, 可以方便的重复Mark
    C-x h      Mark the whole buffer
    C-x u        撤消
    C-x C-a C-l Revert a buffer
    g                刷新Dired mode buffer

    常用查找命令:
    C-s        向前查找
    C-r        向后查找
    M-g M-g Goto a line

    进入Hex mode      M-x hexl-mode ,C-c C-c 退出

    换行:

    不嫌烦的,每次M-x toggle-truncate-lines切换换行与不换行
    一劳永逸的,M-x customize-option,输入truncate-partial-width-windows,将出来的设置页面中的参数改为off,然后保存(Save for future sessions)
    另外说一句,M-x auto-fill-mode也是切换换行模式,不过这是要在文章内容里插入回车符号

    posted @ 2008-07-11 22:16 汉辰 阅读(402) | 评论 (0)编辑 收藏

    2008年7月8日 #

    写这篇东西,是本着好记性不如烂笔头的原则把自己配置LaTex的过程粗略地记录下来以备后查。

    LaTex 是用了20多年的排版工具。因其专业的排版质量和对大文档处理能力,流行于学术界,大多数研究生博士生都用它写自己毕业论文。

    自己3年前曾也想用它写硕士论文,但迫于时间压力,后来还是改用了WYSWYG的StarOffice(Sun的产品,基于OpenOffice)。

    不用Word,因为用它编辑我的90页论文时,总是无可挽回地让我的机器崩溃,当时系统还是Windows2000。

    下面先离题谈一谈近期读程序员修炼之道一书的两个体会,也想阐述一下我为什么对用LaTex统一创建项目文档感兴趣

    1)DRY(Don't Repeat Yourself),这是作者(Andrew Hunt/David Thomas)提到的重要原则,应贯彻到软件开发的各个方面。其中一方面暗示开发流程中应尽可能保持同一级别信息的储备唯一化,而不应有多处载体包含同级别信息。比如从测试规范应可以直接导出TestCase,数据库定义规范直接导出数据库脚本等等。

    2)工欲善其事,必先利其器。专业的程序员除了熟悉自己吃饭用的编程语言外,还需懂得各种辅助工具特别是Perl/Python/Shell一类的脚本语言,因其广泛存在于各种平台中,并有很好的正则表达式和纯文本操纵能力。例如,用他们可以帮助实现项目文档的自动生成和发布等等。比如上面的信息导出就是一个应用目标。

    要达到这样地目的,必须要用纯文本作为信息载体。但是,对于习惯于阅读Word,PDF,HTML等格式化文档的人们来说,纯文本却不太合适。所以3年前的LaTex又浮现在我的眼前。LaTex 很符合DRY原则,文档信息和格式化信息都用纯文本方式保存,但最终发布形式可以用LaTex自动生成,或PDF,或HTML,自己选择。

    LaTex难以学习是一大诟病,但是个人认为,软件开发文档有比较统一的格式化规范,只要有现成的模板,普通程序员可以通过短时间学习掌握基本的编辑能力。

    下面言归正传:

    大家时间都不多,Windows下使用LaTex最方便的办法是安装MiCTex套装,其主要包括MikTex(LaTex的一种实现),SciTE(LaTex文档编辑器),Tex4ht(LaTex文档到Html转换器)等,还捆绑了一些入门学习资料。

    LaTex内核无法识别超越ASCII的编码,中文处理是以外挂的方式进行的。CJK是目前使用的最多的LaTex的中文化包,MiCTex也包括了,不过中文字体还需配置(也可能因为我的系统是英文的XP所致)。

    比较了网上Google到的资料,Helmer Aslaksen的这篇文章最全面和正确的描述了中文配置过程
    Chinese TeX Using the CJK LaTeX Package, Unicode TrueType Fonts and pdfTeX under Windows
    http://www.math.nus.edu.sg/aslaksen/cs/cjk.html

    MiCTex作者最近也提供了一个中文字体自动配置工具,可以很方便的生成各类字体配置文件,并支持UTF8或GBK编码,很好用。我用它安装了系统中的微软宋体,黑体。结果比上面Cyberbit字体好。
    http://bbs.ctex.org/viewthread.php?tid=44496&extra=page%3D1

    下面可以下载到其他免费字体文件
    http://www.wazu.jp/gallery/Fonts_ChineseSimplified.html

    Tex4ht是MiCTex捆绑的LaTex2Html转换工具,英文很不错,中文有点问题,还要下功夫看看那里出问题。我自己很憧憬以HTML的形式发布和更新项目文档,简单实用,这个发布过程可以作为一个步骤加入每晚的Build。

    posted @ 2008-07-08 14:20 汉辰 阅读(1858) | 评论 (0)编辑 收藏

    2008年6月26日 #

    Fork-join framework是Java 7并行库的新内容,基于divide-and-conquer算法来处理大数据量计算。DnQ是处理微粒度并行计算(数据量大,单位运行时间短)的理想模式。数据量在达到一个预定义的门槛之前,被分割成多个任务被Worker threads执行。因为现代Java虚拟机都把Java thread映射到系统线程或LWP(Light-weight process) ,同时Worker数量一般设定等同于CPU个数,这样在多核的硬件系统中能充分利用多个CPU的计算能力。

    写了一个MergeSort的测试例子,最终的排序用的是Java Collection Framework 自带的Arrays.sort()。在自己双核机器试了试,发现提升不是特别明显。Arrays.sort 本身很高效,Framework有thread之间协作和管理worker pool的开销,所以必须选择一个适合的数据量阚值。下面是运行结果:

    java -Xms64m -Xmx128m -cp C;/forkjoin/jsr166y.zip;C:/workspace/java.tij forkjoin.SortTask

    Number of processor 2
    =================Sequential ===================
    Sorting takes 2617701971 to complete
    =================ForkJoin ====================
    Sorting takes 2284940405 to complete

    找不到更多核的机器,有条件的同学可以测试一把。另外,Brain Goetz (Java Concurrency in Practice作者) 的文章可参考,他的测试例子显示了不错的性能提升(最高17倍在32cpu系统),一般4核或8核的能达到3倍或5倍的SPEEDUP

    Java thread and practice: Stick a fork in it Part 1 - http://www.ibm.com/developerworks/java/library/j-jtp11137.html

    package forkjoin;

    import jsr166y.forkjoin.RecursiveAction;
    import jsr166y.forkjoin.ForkJoinPool;
    import java.util.Random;
    import java.util.Arrays;

    public class SortTask extends RecursiveAction {

        
    final static int ARRAY_LENGTH = 10000000;

        
    final static int THRESHOLD = 3000000;

        
    final int[] array;

        
    final int lo;

        
    final int hi;

        
    public SortTask(int[] array, int lo, int hi) {
            
    this.array = array;
            
    this.lo = lo;
            
    this.hi = hi;
        }


        
    private void sequentiallySort(int[] array, int lo, int hi) {
            
    int[] units = new int[hi - lo + 1];
            
    for (int i = lo; i <= hi; i++)
                units[i 
    - lo] = array[i];
            Arrays.sort(units);
            
    for (int i = lo; i <= hi; i++)
                array[i] 
    = units[i - lo];
        }


        
    private void merge(int[] array, int lo, int mid, int hi) {

            
    int[] units = new int[hi - lo + 1];
            
    int i = lo;
            
    int j = mid + 1;

            
    for (int k = 0; k < units.length; k++{
                
    if (array[i] <= array[j])
                    units[k] 
    = array[i++];
                
    else if (array[i] > array[j])
                    units[k] 
    = array[j++];

                
    if (i > mid)
                    
    for (int m = j; m <= hi; m++)
                        units[
    ++k] = array[m];
                
    else if (j > hi)
                    
    for (int m = i; m <= mid; m++)
                        units[
    ++k] = array[m];
            }


            
    for (int k = lo; k <= hi; k++)
                array[k] 
    = units[k - lo];

        }


        
    protected void compute() {
            
    try {
                
    if (hi - lo < THRESHOLD)
                    sequentiallySort(array, lo, hi);
                
    else {
                    
    int mid = (lo + hi) >>> 1;
                    
    //System.out.println(mid);
                    forkJoin(new SortTask(array, lo, mid), new SortTask(array, mid + 1, hi));

                    merge(array, lo, mid, hi);
                }

            }
     catch (Throwable t) {
                t.printStackTrace();
            }

        }


        
    /**
         * 
    @param args
         
    */

        
    public static void main(String[] args) {
            
    int[] sample = new int[ARRAY_LENGTH];

            System.out.println(
    "Number of processor"
                    
    + Runtime.getRuntime().availableProcessors());
            
            Random seed 
    = new Random(47);

            
    for (int i = 0; i < sample.length; i++{
                sample[i] 
    = seed.nextInt();
            }


            
    long start = System.nanoTime();
            Arrays.sort(sample);
            
    long duration = System.nanoTime() - start;


            System.out.println(
    "===============Sequential==================");
            System.out.println(
    "Sorting takes " + duration + " to compelte");

            
    int[] sample2 = new int[ARRAY_LENGTH];

            
    for (int i = 0; i < sample2.length; i++{
                sample2[i] 
    = seed.nextInt();
            }


            ForkJoinPool pool 
    = new ForkJoinPool(Runtime.getRuntime()
                    .availableProcessors());
            SortTask st 
    = new SortTask(sample2, 0, sample2.length - 1);

            start 
    = System.nanoTime();
            pool.execute(st);
            
    while (!st.isDone()) {
            }

            duration 
    = System.nanoTime() - start;

            System.out.println(
    "===============ForkJoin==================");
            System.out.println(
    "Sorting takes " + duration + " to compelte");
            
        }


    }


    posted @ 2008-06-26 10:11 汉辰 阅读(1301) | 评论 (2)编辑 收藏