zoukankan      html  css  js  c++  java
  • paip.提升性能--多核cpu中的java/.net/php/c++编程

    paip.提升性能--多核cpu中的java/.net/php/c++编程




    作者Attilax  艾龙,  EMAIL:1466519819@qq.com 
    来源:attilax的专栏
    地址:http://blog.csdn.net/attilax




    ////////////////目前情况
    需要一个处理50w行的文件,按行进行分词..需要50s才能完成..
    经过调试,瓶颈在这个代码上... TokenStream ts = Analyzer.tokenStream("", reader); 换成IKSegmenter方式,还是一样., // IKSegmenter ik=new IKSegmenter(reader, true);
    机器环境:win7 64bit..cpu双核,2.7g...内存8g
    查看cpu使用,仅仅使用了1个cpu,另外一个空闲着了...要全部使用了,性能应该能行提升2倍了..


     
    ////////////使用多线程来提升多核cpu的性能..
    多核随着硬件的不断升级,作为计算机核心的CPU也改头换面,成为一颗多心的家伙。设计它的专家声称这可以最大限度地提高程序的运行效率。但事实真的如此吗?众所周知,现在的程序大多都是基于单线程而设计的,然而这种设计模式如果在主频高的单核CPU上运行,效率是非常高的。但它并不能很好地协调多核一起工作。


    更不幸的是单线程的程序在多核CPU上运行甚至可能会没有单核CPU快。


    当然,就目前而言,要解决这种性能软瓶颈的最佳方法就是本专题所讨论的多线程编程。只有充分利用这种技术,才能发挥多核的最大潜力。好了,就说到这吧,最后让我们利用本专题所讲的内容尽情地享受多核给我们带来的惊喜和刺激吧!




    2、线程分解和执行的区别:
    对单核CPU,对客户端软件,采用多线程,主要是 创建多线程将一些计算放在后台执行,而不影响用户交互操作。(用户界面 & 其他计算 并行进行)提高用户的操作性能!


    多核中,分别出多个线程,不再限于将用户界面操作和其他计算分离。分解多个线程使为了让计算分配到各CPU上执行。执行线程数量与CPU核数有关!如果线程数小于核数,某些CPU肯定处于空闲状态。


    3、CPU核负载平衡:
    单核中不考虑 负载平衡,各个线程计算量相差很大,也不会影响程序总计算时间。
    多核中,必须考虑将各个线程计算量均衡到各CPU


    7、串行计算与并行计算、分布式计算的区别
    单核多线程编程中,都是串行算法。用不到并行计算,更用不到分布式计算
    多核编程中,多采用 并行计算 和 分布式计算


    并行计算=并行设计模式+并行算法
    分布式计算复杂度 > 并行计算的复杂度 > 串行计算复杂度


    并行计算:只考虑计算并行地执行,不考虑线程间的竞争导致CPU饥饿
    分布式计算:相比并行计算,能更好解决CPU饥饿,使计算均衡地分配任务到各内核


    /////////////////代码如下:
    经过测试,因为仅仅有2个cpu核心,走凯里2个线程..果然,2个cpu占用都块道理100%的兰,时间一挂噪城里原来的一半儿..25s走ok兰...
    List<String> lica7 = fc.fileRead2list(sourceTxt, "utf-8");
    int mid = lica7.size() / 2;
    final List<String> lica7_half1 = lica7.subList(0, mid);
    List<String> lica7_half2 = lica7.subList(mid, lica7.size());
    new Thread>>>>
    lica7_half1_result = ftc.fentsiMuilttheard(lica7_half1,
    wordLibs, "thd1");
    half1_isfinesh=true;
    //main thread
    //wait 4 half_thread_finish
    //habin lyag list..




    /////////////true code.





    List<String> lica7_half1_result;
    boolean half1_isfinesh=false;


    /**
    * @author attilax 1466519819@qq.com
    * @param sourceTxt
    * @param targetTxt
    * @param wordLibs
    *            for file line >5w bacause 50w time tooo long ,gujyi
    *            1min...muset rewrite jeig swefa.
    * @since cae
    */
    public void fentsi4bigfile(String sourceTxt, String targetTxt,
    final String wordLibs) {


    // String wordLibs =
    // "c:\wordFromInputmethod.txt,c:\word.txt,c:\ext.txt";
    // String sourceTxt = "c:\sincin.txt";
    // String targetTxt = "c:\fentsiOK.txt";
    int n = 0;
    List<String> li_r = new ArrayList<String>();


    fileC0 fc = new fileC0();
    // String text = fc.fileRead(sourceTxt);
    // if(true)return;
    List<String> lica7 = fc.fileRead2list(sourceTxt, "utf-8");
    int mid = lica7.size() / 2;
    final List<String> lica7_half1 = lica7.subList(0, mid);
    List<String> lica7_half2 = lica7.subList(mid, lica7.size());


    List<String> lica7_half2_result;
    // n = fentsiMuilttheard(wordLibs, n, lica7);


    new Thread(new Runnable() {
    public void run() {


    try {
    fentsiOr ftc = new fentsiOr();
    lica7_half1_result = ftc.fentsiMuilttheard(lica7_half1,
    wordLibs, "thd1");
    half1_isfinesh=true;


    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName());


    }
    }).start();



    //main thread
    fentsiOr ftc = new fentsiOr();
    lica7_half2_result = ftc.fentsiMuilttheard(lica7_half2, wordLibs,
    "thd_main");



    //stop whiel half is not ok
    int nn = 0;
    while (true) {
    if (half1_isfinesh) {
    break;
    }
    nn++;
    try {
    Thread.sleep(50);
    System.out.println("--sleep 500 ,num:" + nn);
    } catch (InterruptedException e) {


    e.printStackTrace();
    throw new RuntimeException(e);
    }


    }


    List<String> li_all=new ArrayList<String>();
    li_all.addAll(lica7_half1_result);
    li_all.addAll(lica7_half2_result);
     fc.saveList2file(li_all, targetTxt, "utf-8");


    }








    参考:
    多核编程 与 单核多线程编程的区别 - 点滴记录 不断进步 - 博客频道 - CSDN.NET.htm
  • 相关阅读:
    Delphi 2009 泛型容器单元(Generics.Collections)[5]: TObject...<T> 系列
    Delphi 2009 中 TStrings 与 TStream 的增强
    Delphi 2009 泛型容器单元(Generics.Collections)[4]: TDictionary<T>
    Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>
    Delphi 2009 中 string 与 Char 的改变
    Delphi 2009 的反射单元(ObjAuto):
    Delphi 2009 泛型容器单元(Generics.Collections)[3]: TStack<T>
    博客园电子期刊2010年4月刊发布啦
    上周热点回顾(5.105.16)
    上周热点回顾(4.265.2)
  • 原文地址:https://www.cnblogs.com/attilax/p/5964173.html
Copyright © 2011-2022 走看看