美文网首页
java-go-php测试循环计算读写内存性能

java-go-php测试循环计算读写内存性能

作者: yichen_china | 来源:发表于2024-09-22 09:17 被阅读0次

    go,java比较快,php比较慢
    下面是java代码

        public static void test(){
           long start =System.currentTimeMillis();
            System.out.println( start);
           int count=1000000000;
           int num=0;
           long end =0;
           for(int i =0;i<count;i++){
               num++;
               end = new Date().getTime()-start;
           }
    //        end = System.currentTimeMillis();
           System.out.println(end);
            start =System.currentTimeMillis();
             num=0;
            for(int i =0;i<count;i++){
                ++num;
                long end2 =new Date().getTime();
                end = end2-start;
            }
            System.out.println(end);
            start =System.currentTimeMillis();
            num=0;
           while (count>num){
                ++num;
                long end2 =new Date().getTime();
                end = end2-start;
            }
            System.out.println(end);
        }
    

    处理时间毫秒:
    5434
    5168
    5215

        public static void test(){
            long start =System.currentTimeMillis();
            System.out.println( start);
            int count=100000000;
            int num=0;
            long end =0;
            List<Integer> arr = new ArrayList<>();
            for(int i =0;i<count;i++){
                num++;
                arr.add(num);
    //            end = new Date().getTime()-start;
            }
    //        arr=new ArrayList<>();
            end = new Date().getTime()-start;
            System.out.println(end);
            System.out.println(arr.size());
        }
    

    结果

    1727054001864
    56387
    100000000
    

    go

    func main() {
        count := 1000000000
    
        num := 0
        var str string
        var end int64
    
        start := time.Now().UnixNano() / 1e6
        num = 0
        for i := 0; i < count; i++ {
            num = 1 + num
            //str = ""
            //var str2 = "a法大师傅大师傅法大师傅大师傅"
            //str = str2
            end2 := time.Now().UnixNano()/1e6 - start
            end = end2
        }
        end = time.Now().UnixNano()/1e6 - start
        log.Println("for ++i:{d};{d}", end, num, str)
    
        start = time.Now().UnixNano() / 1e6
        log.Println(start)
        for i := 0; i < count; i++ {
            num++
            end = time.Now().UnixNano()/1e6 - start
        }
        end = time.Now().UnixNano()/1e6 - start
        log.Println("for i++:{d};{d}", end, num)
        log.Fatalln()
        start = time.Now().UnixNano() / 1e6
        log.Println(start)
        for i := 0; i < count; i++ {
            num++
            str = ""
            var str2 = "a法大师傅大师傅法大师傅大师傅"
            str = str2
            end = time.Now().UnixNano()/1e6 - start
        }
        end = time.Now().UnixNano()/1e6 - start
        log.Println("for i++:{d};{d}", end, num)
        start = time.Now().UnixNano() / 1e6
        num = 0
        for num < count {
            num = 1 + num
            str = ""
            var str2 = "a法大师傅大师傅法大师傅大师傅"
            str = str2
            end = time.Now().UnixNano()/1e6 - start
        }
        end = time.Now().UnixNano()/1e6 - start
        log.Println("for ++i:{d};{d}", end, num, str)
        start = time.Now().UnixNano() / 1e6
        num = 0
        for num < count {
            num++
            str = ""
            var str2 = "a法大师傅大师傅法大师傅大师傅"
            str = str2
            end = time.Now().UnixNano()/1e6 - start
        }
        end = time.Now().UnixNano()/1e6 - start
        log.Println("for ++i:{d};{d}", end, num, str)
    }
    
    

    处理时间毫秒是java的1/3

    2024/09/22 12:05:30 for ++i:{d};{d} 15724 1000000000 
    2024/09/22 12:05:30 1726977930443
    2024/09/22 12:05:45 for i++:{d};{d} 15430 2000000000
    
        start = time.Now().UnixNano() / 1e6
        log.Println(start)
        var arr []int
        for i := 0; i < count; i++ {
            num++
            //end = time.Now().UnixNano()/1e6 - start
            arr = append(arr, num)
        }
        //arr = nil
        end = time.Now().UnixNano()/1e6 - start
        log.Println("for i++:{d};{d}", end, num, len(arr))
    

    结果

    2024/09/23 09:15:52 1727054152806
    2024/09/23 09:15:53 for i++:{d};{d} 1062 200000000 100000000
    

    php 处理时间=go的10分之一

    下面是php的测试
    php循环1亿次,java和go是循环10亿次,同样精确到毫秒

        public function test()
        {
            $num=0;
            $time=intval( time() *1000 +microtime(true));
            $data=[];
            echo $time;
            print_r( "\n");
            $ts=100000000;
            while ($num< $ts){
                ++$num;
                $time2=intval(time() *1000 +microtime(true));
            }
            $time2=intval(time() *1000 +microtime(true));
            $data['++i']=[$time2-$time];
    
            $time=intval( time() *1000 +microtime(true));
            $num=0;
            while ($num< $ts){
                $num++;
                $time2=intval(time() *1000 +microtime(true));
    
            }
            $time2=intval(time() *1000 +microtime(true));
            $data['i++']=[$time2-$time];
    //        $data['substring']=$substring;
            print_r($data);
    //        $text =(new Chat)->outFilter($word);
    //        print_r(  $text);
            exit();
    

    结果

      1728704477500
    Array
    (
        [++i] => Array
            (
                [0] => 12012
            )
    
        [i++] => Array
            (
                [0] => 13013
            )
    
    )
    

    php测试2 测试循环读写内存

        public function test()
        {
            $num=0;
            $time=intval( time() *1000 +microtime(true));
            $data=[];
            echo $time;
            print_r( "\n");
            $ts=100000000;
            while ($num< $ts){
                ++$num;
                $str = "Hello, World!";
                unset($str );
                $str = "Hello, World!";
                unset($str );
    //            $time2=intval(time() *1000 +microtime(true));
    //            $str = "Hello, World!";
    //            $substring = substr($str, 0, 5); // 截取从第0个字符开始的5个字符
            }
            $time2=intval(time() *1000 +microtime(true));
            $data['++i']=[$time2-$time];
    
            $time=intval( time() *1000 +microtime(true));
            $num=0;
            while ($num< $ts){
                $num++;
    //            $time2=intval(time() *1000 +microtime(true));
                $str = "Hello, World!";
                unset($str );
                $str = "Hello, World!";
                unset($str );
    //            $substring = substr($str, 0, 5); // 截取从第0个字符开始的5个字符
            }
            $time2=intval(time() *1000 +microtime(true));
            $data['i++']=[$time2-$time];
    //        $data['substring']=$substring;
            print_r($data);
    //        $text =(new Chat)->outFilter($word);
    //        print_r(  $text);
            exit();
    

    php循环1亿次,java和go是循环10亿次,同样精确到毫秒

    结果

    Array
    (
        [++i] => Array
            (
                [0] => 2002
            )
    
        [i++] => Array
            (
                [0] => 2002
            )
    
    )
    
    

    相关文章

      网友评论

          本文标题:java-go-php测试循环计算读写内存性能

          本文链接:https://www.haomeiwen.com/subject/otgzljtx.html