A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 胡建伟   /  2013-8-26 21:07  /  16505 人查看  /  40 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Test2
{

        public static void main(String[] args)
        {       
                FunctionOne();
                FunctionTwo();
                for(int i=1;i<10;i++)//照理来说应该是位移运算是最快的,可是在我机器运行,多次运行!然后比较2者运算时间不确定
                {
                        FunctionOne();
                        FunctionTwo();
                }                       
        }

        private static void  FunctionOne()//方法1:直接相乘 看处理器时间
        {
                int result=0;//保存结果               
                long pre=System.currentTimeMillis();//当前时间
                long post=0;//保存运行结束后时间
                for(int i=0;i<100000000;i++)//运行1亿次,看运行时间
                         result = 3*8;
                post=System.currentTimeMillis();
                System.out.println("方法1 运行时间为:"+(post-pre)+" ms"+"  3乘8的结果为:"+result);
        }
       
        private static void FunctionTwo()//通过位移 左移3位,相当于成8,理论上速度更快!
        {
                int result=0;//保存结果               
                long pre=System.currentTimeMillis();//当前时间
                long post=0;//保存运行结束后时间
                for(int i=0;i<100000000;i++)//运行1亿次,看运行时间
                         result = 3<<3;
                post=System.currentTimeMillis();
                System.out.println("方法2 运行时间为:"+(post-pre)+" ms"+"  3乘8的结果为:"+result);
        }
               
}
回复 使用道具 举报
123
您需要登录后才可以回帖 登录 | 加入黑马