平行計算--Java--求π並行實現
阿新 • • 發佈:2019-01-27
- publicvoid run()
- {
- int i;
- step=1.0/(double)num_steps_wy;
- for(i=start_wy;i<num_steps_wy;i+=2)
- {
- x=(i+0.5)*step;
- sum=sum+4.0/(1.0+x*x);
- }
- }
- publicvoid seril_pi()
- {
- int i;
- step=1.0/(double)num_steps_wy;
- for(i=1;i<num_steps_wy;i++)
- {
-
x=(i+0.5
- sum=sum+4.0/(1.0+x*x);
- }
- }
- publicstaticvoid main(String[] args) throws InterruptedException {
- double pi_wy,sum_wy=0.0,seri_t_wy,para_t_wy;
- Pi_thread thread1=new Pi_thread(1);
- Pi_thread thread2=new Pi_thread(2);
- double t1=System.currentTimeMillis();
-
thread1.start();
- thread2.start();
- thread1.join();
- thread2.join();
- double t2=System.currentTimeMillis();
- sum_wy=thread1.sum+thread2.sum;
- pi_wy=thread1.step*sum_wy;
- para_t_wy=t2-t1;
- System.out.println("並行結果: "+pi_wy);
- System.out.println("並行時間: "+para_t_wy);
-
Pi_thread thread3=new Pi_thread(
- t1=System.currentTimeMillis();
- thread3.seril_pi();
- t2=System.currentTimeMillis();
- pi_wy=thread3.sum*thread3.step;
- seri_t_wy=t2-t1;
- System.out.println("竄行結果: "+pi_wy);
- System.out.println("序列時間: "+seri_t_wy);
- System.out.println("加速比: "+(seri_t_wy/para_t_wy));
- }
- }
並行結果: 3.1415926135900225
並行時間: 484.0
竄行結果: 3.1415926135904266
序列時間: 1000.0
加速比: 2.0661157024793386
- publicvoid run()
- {
- int i;
- step=1.0/(double)num_steps_wy;
- for(i=start_wy;i<num_steps_wy;i+=2)
- {
- x=(i+0.5)*step;
- sum=sum+4.0/(1.0+x*x);
- }
- }
- publicvoid seril_pi()
- {
- int i;
- step=1.0/(double)num_steps_wy;
- for(i=1;i<num_steps_wy;i++)
- {
- x=(i+0.5)*step;
- sum=sum+4.0/(1.0+x*x);
- }
- }
- publicstaticvoid main(String[] args) throws InterruptedException {
- double pi_wy,sum_wy=0.0,seri_t_wy,para_t_wy;
- Pi_thread thread1=new Pi_thread(1);
- Pi_thread thread2=new Pi_thread(2);
- double t1=System.currentTimeMillis();
- thread1.start();
- thread2.start();
- thread1.join();
- thread2.join();
- double t2=System.currentTimeMillis();
- sum_wy=thread1.sum+thread2.sum;
- pi_wy=thread1.step*sum_wy;
- para_t_wy=t2-t1;
- System.out.println("並行結果: "+pi_wy);
- System.out.println("並行時間: "+para_t_wy);
- Pi_thread thread3=new Pi_thread(3);
- t1=System.currentTimeMillis();
- thread3.seril_pi();
- t2=System.currentTimeMillis();
- pi_wy=thread3.sum*thread3.step;
- seri_t_wy=t2-t1;
- System.out.println("竄行結果: "+pi_wy);
- System.out.println("序列時間: "+seri_t_wy);
- System.out.println("加速比: "+(seri_t_wy/para_t_wy));
- }
- }
並行結果: 3.1415926135900225
並行時間: 484.0
竄行結果: 3.1415926135904266
序列時間: 1000.0
加速比: 2.0661157024793386