關於C和C++,還有c#,還有java程式的速度問題!(四)

來源:互聯網
上載者:User

這裡給出java測試代碼

 

1.Stopwatch類的實現

 

//filename:StopWatch.java<br />package my.test;<br />public class StopWatch<br />{<br />private long startTime = 0;<br /> private long stopTime = 0; </p><p> public void start() {<br /> this.startTime = System.currentTimeMillis(); </p><p> } </p><p> public void stop() {<br /> this.stopTime = System.currentTimeMillis(); </p><p> } </p><p> //elaspsed time in milliseconds<br /> public long getElapsedTime() {<br /> long elapsed; </p><p> elapsed = (stopTime - startTime); </p><p> return elapsed;<br /> } </p><p>}<br /> 

 

2.測試工作類的實現

 

//filename:WorkClass.java<br />package my.test;<br />public class WorkClass<br />{<br />public double AddTest()<br />{<br />int count=10000000,i=0,j=0,count2=10;<br /> double sum=0.0;<br /> for(i=0;i<count2;i++)<br /> for(j=0;j<count;j++)<br /> sum+=j;<br /> return sum;<br />}</p><p>public double MulTest()<br />{<br />int count=10000*10000,i=0;<br /> double sum=0.0;<br /> for(i=1;i<count;i++)sum*=i;<br /> return sum;<br />}</p><p>public double DivTest()<br />{<br />int count=10000*10000,i=0;<br /> double sum=0.0;<br /> for(i=1;i<count;i++)sum/=i;<br /> return sum;<br />}</p><p>private long fib(long n)<br />{<br />if (n < 2)<br />return (1);<br />else<br />return (fib(n - 2) + fib(n - 1));<br />}</p><p>public double FibTest()<br />{<br />return (double)fib(30);<br />}<br />}<br /> 

 

3.自動化的測試類

//filename:AutoTestClass.java<br />package my.test;<br />import java.lang.reflect.Method;<br />public class AutoTestClass<br />{<br />public void AutoTest(String itemname)<br />{<br /> int test_count=10;<br /> double test_time=0.0;<br /> int run_count=0;<br /> Double ret;<br /> StopWatch sw=new StopWatch();<br /> WorkClass wc=new WorkClass();</p><p> try<br /> {<br /> Class myclass = Class.forName("my.test.WorkClass");<br /> Method mytest_fun = myclass.getMethod(itemname);<br /> for(run_count=0;run_count<test_count;run_count++)<br /> {<br /> ret=0.0;<br /> sw.start();<br /> ret=(Double)mytest_fun.invoke(wc,null);</p><p> if(ret>0)sw.stop();<br /> else sw.stop();</p><p> test_time+=sw.getElapsedTime();<br /> }<br /> }catch (Throwable e)<br /> {<br /> System.err.println(e);<br /> }</p><p> String outstr="work cost average time(10)="+test_time/test_count;<br /> System.out.println(outstr);<br />}<br />}<br /> 

 

4.最後是主程式類

//filename:MainClass.java<br />package my.test;<br />public class MainClass {<br />/**<br /> * @param args<br /> */<br />public static void main(String[] args)<br />{<br />AutoTestClass atc=new AutoTestClass();<br />atc.AutoTest("AddTest");<br />atc.AutoTest("FibTest");<br />atc.AutoTest("MulTest");<br />atc.AutoTest("DivTest");</p><p>}<br />}<br /> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.