Bonus speed - Profiling - 快!(下)
上一集我地高呼左cProfiler (python)
今集會集中講下java
首先,小伙子我未有同java結成伴侶。 因為未有火花住。所以還望大家指教一下如有不是!
前提
如果要將programming lang分開派別。我地會有:
compiler language (Java, c, obj-c)
Interpreter language (php, python, JavaScript )
OR both! (Java, python, .Net)
用膚淺既角度去理解就是
Compiled Lang is faster than Interpreted Lang in terms of execution time.
用深入既角度去明白就是:
Language is not related to running time. It's the compiler and interpreter making it execute fast.
好奇怪同好混亂! 我地今次探討既野唔係compiler (下次再講),今次講既係有咩元素會影響到execution time(speed)。
正提:
係java既世界,我地要搵一個profiler好鬼易,例如VisualVM同埋jvmmonitor。兩個都係免費,兩個都係做同一樣野,你自己選擇啦~
今次講既係 HPROF
Why? 因為內置,command line,冇野需要set就已經出到一些好好既資料
首先import左一堆要用既野先
以下段code就係上次我地用python multiprocessing整出來既野,不同之處係我地用multithreading,係咪因為java冇multiprocessing呢?!非也~ 咁分別係邊到呀!?(下回分解)
睇完上面段code唔明既朋友,而家講解下先!
我地先唔好理 Mod_check_callable() 係咩來先,我地當佢係一個function.
好Mod_check_callable 如下:
最後行個main,感覺同C++/Python冇咩唔一樣,除左要開class之外
我地會係terminal行句command:
行完句command就會見到有個file “java.hprof.txt”, 我地cat 睇下:
你會見到有rank分左邊個function用左最多cpu time,但係留意,用得多唔一定係問題所在,因為我地今次行緊thread,所以個比重一定唔會同冇行thread既比較到 . (咁點樣先真正計算出每個thread既exeution time呢? ? ?)
當然啦HPROF 唔止咁樣ge~ 佢仲有dump heap 既toools用到。 有機會試下其他paramater。
係咪同上集有好大分別呢!你會見到我地java會係300ms左右做曬所以method, 原因係我地用緊java compiler變左做byte-code, 部機就有個java vm去令byte-code 變返做assembly lang,我地cpu就係好快咁run個result出來。
比起python要1s以上先可以run完一個method。。。
先compile後run既分別我地遲d先再講,並唔係一句表達曬出來。
總結:
今集講既profiler好易用,但係present出來既效果唔多靚仔,建議大家試下VisualVM, jvmmonitor. 試過既朋友reply下,post下有冇咩溜左~期待!
Profiling 講到e道先~ 下集係multiprocessing 同multithreading 既野,
Source:
https://github.com/zeptoTon/project-euler
Reference:
http://docs.oracle.com/javase/7/docs/technotes/samples/hprof.html http://www.vogella.com/tutorials/JavaPerformance/article.html http://docs.oracle.com/javase/tutorial/essential/concurrency/highlevel.html http://martiansoftware.com/nailgun/background.html
後記。
發覺出blog有樣野好煩,就係要人地估要用幾多時間睇/了解,10分鐘?15分鐘?
當然啦, 我可以學人地blog engine咁有個read time 提示,同人講10分鐘睇得曬。又或者。。。。係youtube出vlog,下面加返中英scripts,又睇得又讀得!
今集會集中講下java
首先,小伙子我未有同java結成伴侶。 因為未有火花住。所以還望大家指教一下如有不是!
前提
如果要將programming lang分開派別。我地會有:
compiler language (Java, c, obj-c)
Interpreter language (php, python, JavaScript )
OR both! (Java, python, .Net)
用膚淺既角度去理解就是
Compiled Lang is faster than Interpreted Lang in terms of execution time.
用深入既角度去明白就是:
Language is not related to running time. It's the compiler and interpreter making it execute fast.
好奇怪同好混亂! 我地今次探討既野唔係compiler (下次再講),今次講既係有咩元素會影響到execution time(speed)。
正提:
係java既世界,我地要搵一個profiler好鬼易,例如VisualVM同埋jvmmonitor。兩個都係免費,兩個都係做同一樣野,你自己選擇啦~
今次講既係 HPROF
Why? 因為內置,command line,冇野需要set就已經出到一些好好既資料
首先import左一堆要用既野先
/*
P1 Multiples of 3 and 5
Find the sum of all the multiples of 3 or 5 below 1000.
We exec this question in multithreading way in java
There are different way to do multiThreading
using ThreadPool with Callable and Future object
p.s Runnable is not for return type function.
*/
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.ArrayList;
import java.util.List;
跟住開個p1 class,寫返上次我地用過既方法,formula,for loop
public class p1 {
private static final int NTHREDS = 10;
private static final int BOUND = 100000000;
private static int STEP = BOUND/NTHREDS;
public static long fastest_way(){
long n = (BOUND-1)/3;
long m = (BOUND-1)/5;
long r = (BOUND-1)/(5*3);
return (3*(n+1)*n/2) + (5*(m+1)*m/2) - (15*(r+1)*r/2);
}
public static long alternate_method_forloop(){
long a = 0;
for ( int i=0; i<BOUND; i++ ) {
if (i%3==0 || i%5==0) {
a += i;
}
}
return a;
}
以下段code就係上次我地用python multiprocessing整出來既野,不同之處係我地用multithreading,係咪因為java冇multiprocessing呢?!非也~ 咁分別係邊到呀!?(下回分解)
public static long alternate_method_multthreading(){
List<Future<Long>> list = new ArrayList<Future<Long>>();
/* Magic Happen */
ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
for (int i=0; i<NTHREDS ; i++ ) {
/* Mod_check_callable ?! What is it? */
Callable<Long> worker = new Mod_check_callable(i*STEP, STEP);
Future<Long> submit = executor.submit(worker);
list.add(submit);
}
long sum = 0;
// now retrieve the result
for (Future<Long> future : list) {
try {
sum += future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
executor.shutdown();
return sum;
}
睇完上面段code唔明既朋友,而家講解下先!
我地先唔好理 Mod_check_callable() 係咩來先,我地當佢係一個function.
- 我地開個ThreadPool (同上次python一樣,開pool)
- 我地開10個worker (因為有10個thread),每個worker assign左mod_check個function,個step既意思係佢地worker 與worker之間check唔同既數,i.e. worker1 check 1-1000, worker2 check 1001-2000, 如此類推
- 將啲worker放入個threadPool到,thread pool會見到有worker入左來,assign個thread比地方佢做野。
- 當個worker入左thread pool 做完要做既野,我地要安排一個地方比佢return返個value,因為會有好多個worker return value,咁我地用array去迎接佢地就最好不過啦~
- 最後,我地會loop個array,加返曬所有既return value就會出左個answer啦!!
好Mod_check_callable 如下:
import java.util.concurrent.Callable;
public class Mod_check_callable implements Callable<Long>{
private final long i;
private final long step;
Mod_check_callable(long i, long step){
this.i = i;
this.step = step;
}
@Override
public Long call() throws Exception {
long a = 0;
for (long i=this.i; i<this.i+this.step ; i++) {
if (i%3==0 || i%5==0) {
a += i;
}
}
return a;
}
}
最後行個main,感覺同C++/Python冇咩唔一樣,除左要開class之外
public static void main(String[] args){
long start = System.currentTimeMillis();
System.out.println(fastest_way());
double elapsedTimeInSec = (System.currentTimeMillis() - start);
System.out.println("formula:"+elapsedTimeInSec);
start = System.currentTimeMillis();
System.out.println(alternate_method_forloop());
elapsedTimeInSec = (System.currentTimeMillis()- start);
System.out.println("forloop:"+elapsedTimeInSec);
start = System.currentTimeMillis();
System.out.println(alternate_method_multthreading());
elapsedTimeInSec = (System.currentTimeMillis()- start);
System.out.println("multithread:"+elapsedTimeInSec);
}
我地會係terminal行句command:
java -agentlib:hprof=cpu=samples p1
行完句command就會見到有個file “java.hprof.txt”, 我地cat 睇下:
你會見到有rank分左邊個function用左最多cpu time,但係留意,用得多唔一定係問題所在,因為我地今次行緊thread,所以個比重一定唔會同冇行thread既比較到 . (咁點樣先真正計算出每個thread既exeution time呢? ? ?)
當然啦HPROF 唔止咁樣ge~ 佢仲有dump heap 既toools用到。 有機會試下其他paramater。
係咪同上集有好大分別呢!你會見到我地java會係300ms左右做曬所以method, 原因係我地用緊java compiler變左做byte-code, 部機就有個java vm去令byte-code 變返做assembly lang,我地cpu就係好快咁run個result出來。
比起python要1s以上先可以run完一個method。。。
先compile後run既分別我地遲d先再講,並唔係一句表達曬出來。
總結:
今集講既profiler好易用,但係present出來既效果唔多靚仔,建議大家試下VisualVM, jvmmonitor. 試過既朋友reply下,post下有冇咩溜左~期待!
Profiling 講到e道先~ 下集係multiprocessing 同multithreading 既野,
Source:
https://github.com/zeptoTon/project-euler
Reference:
http://docs.oracle.com/javase/7/docs/technotes/samples/hprof.html http://www.vogella.com/tutorials/JavaPerformance/article.html http://docs.oracle.com/javase/tutorial/essential/concurrency/highlevel.html http://martiansoftware.com/nailgun/background.html
後記。
發覺出blog有樣野好煩,就係要人地估要用幾多時間睇/了解,10分鐘?15分鐘?
當然啦, 我可以學人地blog engine咁有個read time 提示,同人講10分鐘睇得曬。又或者。。。。係youtube出vlog,下面加返中英scripts,又睇得又讀得!
Don't be afraid 別掉眼淚
Don't be afraid 別怕衰
Just be your faith 夢想一歲一歲去追
Comments
Post a Comment