zoukankan      html  css  js  c++  java
  • 两个数交换

    可以通过异或,或者中间变量的方式。以下比较两者的速度,各执行十亿次。

    JRE环境:

    java version "1.7.0_79"
    Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
    Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
    public class Main {
    
        public static final void swap1(int[] a, int i, int j) {
            a[i] = a[i] ^ a[j];
            a[j] = a[i] ^ a[j];
            a[i] = a[i] ^ a[j];
        }
    
        public static final void swap2(int[] a, int i, int j) {
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    
        public static void main(String[] args) {
            int[] a = new int[] {13, 345};
            int times = 1000000000;
            long end;
            long start;
            start = System.currentTimeMillis();
            for (int i = 0; i < times; i++) {
                swap1(a, 0, 1);
            }
            end = System.currentTimeMillis();
            System.out.println(end - start);
            start = System.currentTimeMillis();
            for (int i = 0; i < times; i++) {
                swap1(a, 0, 1);
            }
            end = System.currentTimeMillis();
            System.out.println(end - start);
        }
    }

    输出:

    911
    906

    可以看到后面的swap1方法比前面的快一些,说明JVM对代码进行了运行时优化。把swap1方法替换成swap2方法后:

        public static void main(String[] args) {
            int[] a = new int[] {13, 345};
            int times = 1000000000;
            long end;
            long start;
            start = System.currentTimeMillis();
            for (int i = 0; i < times; i++) {
                swap2(a, 0, 1);
            }
            end = System.currentTimeMillis();
            System.out.println(end - start);
            start = System.currentTimeMillis();
            for (int i = 0; i < times; i++) {
                swap2(a, 0, 1);
            }
            end = System.currentTimeMillis();
            System.out.println(end - start);
        }

    输出:

    556
    549

    可见,使用中间变量的方法使异或的方法要一些。

    把swap2方法的temp变量提到外面,省去每次声明的花费,即:

        private static int temp;
        public static final void swap2(int[] a, int i, int j) {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }

    此时输出为:

    573
    564

    由此可见,在栈上声明变量比使用永久代上的变量要快一些。(static变量存储在永久代,这里指的是hotspot5,6,7的虚拟机,另见where is a static method and a static variable stored in java. In heap or in stack memory

    另外把swap2改成成员方法,把temp改成成员变量:

        private int temp;
        public void swap2(int[] a, int i, int j) {
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }

    输出为:

    576
    564

    和上一个方法速度差不多。使用堆上的变量跟使用永久代上的变量差不多。

  • 相关阅读:
    android 入门-工程属性介绍
    android 入门-android属性介绍
    android 入门-布局
    android 入门-动画与容器
    android 入门- 词汇
    android 入门-引用库项目
    android 入门-防微信拍摄视频 按钮事件处理
    WPF Adorner
    c# Quartz.net的简单封装
    WPF 触摸屏小键盘样式
  • 原文地址:https://www.cnblogs.com/drizzlewithwind/p/6889069.html
Copyright © 2011-2022 走看看