java中for循环和arraycopy的速度问题

「已注销」 2019-03-28 10:37:20
数组中删除一个元素,for循环遍历前移跟arraycopy的自我拷贝,for循环几乎耗时为0而数组拷贝都是有耗时的,为什么都说arraycopy的速度比for循环快,并且ArrayList里的remove操作后的前移也是使用arraycopy去做。
...全文
254 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
NinjaYinJey 2019-04-03
  • 打赏
  • 举报
回复
假如解决了你的问题那么结贴是一个好习惯。
NinjaYinJey 2019-04-03
  • 打赏
  • 举报
回复
引用 2 楼 彩笔梳子 的回复:
那么请问为啥速度效率上跟for循环在我这实验时更快
秒请求量过10w你就知道了。
十八道胡同 2019-04-03
  • 打赏
  • 举报
回复
引用 2 楼 彩笔梳子 的回复:
[quote=引用 1 楼 NinjaYinJey 的回复:] for --> cpu需要单个计算地址 arraycopy --> cpu计算一个连续的内存地址且一次性创建相同大小的内存
那么请问为啥速度效率上跟for循环在我这实验时更快[/quote] 试验代码贴下?
「已注销」 2019-04-03
  • 打赏
  • 举报
回复
引用 1 楼 NinjaYinJey 的回复:
for --> cpu需要单个计算地址 arraycopy --> cpu计算一个连续的内存地址且一次性创建相同大小的内存
那么请问为啥速度效率上跟for循环在我这实验时更快
NinjaYinJey 2019-04-03
  • 打赏
  • 举报
回复
for --> cpu需要单个计算地址 arraycopy --> cpu计算一个连续的内存地址且一次性创建相同大小的内存
「已注销」 2019-04-03
  • 打赏
  • 举报
回复
引用 8 楼 LCL_data 的回复:
 public static void main(String[] args){
        final int LENGTH=20000000;
        int[] s=new int[LENGTH];
        int[] s2=new int[LENGTH];
        for(int i=0;i<LENGTH;i++){
            s[i]=i;s2[i]=i;
        }
        System.out.println("测试for循环:");
        long st1=System.currentTimeMillis();
        for(int i=1;i<LENGTH;i++){
            s[i]=s2[i];
        }
        long ed1=System.currentTimeMillis();
        System.out.println(ed1-st1);

        System.out.println("测试copy循环:");
        long st2=System.currentTimeMillis();
        System.arraycopy(s,1,s,0,s.length-1);
        long ed2=System.currentTimeMillis();
        System.out.println(ed2-st2);

        System.out.println("测试copy循环:");
        st2=System.currentTimeMillis();
        System.arraycopy(s,1,s2,0,s.length-1);
        ed2=System.currentTimeMillis();
        System.out.println(ed2-st2);

        String[] srcArray = new String[1000000];
        String[] forArray = new String[srcArray.length];
        String[] arrayCopyArray  = new String[srcArray.length];

        //初始化数组
        for(int index  = 0 ; index  < srcArray.length ; index ++){
            srcArray[index] = String.valueOf(index);
        }

        long forStartTime = System.currentTimeMillis();
        for(int index  = 0 ; index  < srcArray.length ; index ++){
            forArray[index] = srcArray[index];
        }
        long forEndTime = System.currentTimeMillis();
        System.out.println("for方式复制数组:"  + (forEndTime - forStartTime));

        long arrayCopyStartTime = System.currentTimeMillis();
        System.arraycopy(srcArray,0,arrayCopyArray,0,srcArray.length);
        long arrayCopyEndTime = System.currentTimeMillis();
        System.out.println("System.arraycopy复制数组:"  + (arrayCopyEndTime - arrayCopyStartTime));
    }
我跑了很多次,int数组的效率有时for高,有时arraycopy高。 但是String数组,每次都是arrayCopy高,我在想,是不是arrayCopy在处理引用类型时,只拷贝了其引用导致的?就是都是浅拷贝导致的? https://blog.csdn.net/wangyangzhizhou/article/details/79504818
咦对哦是这个样子的,那为啥数值型for循环更快点呢
十八道胡同 2019-04-03
  • 打赏
  • 举报
回复
 public static void main(String[] args){
        final int LENGTH=20000000;
        int[] s=new int[LENGTH];
        int[] s2=new int[LENGTH];
        for(int i=0;i<LENGTH;i++){
            s[i]=i;s2[i]=i;
        }
        System.out.println("测试for循环:");
        long st1=System.currentTimeMillis();
        for(int i=1;i<LENGTH;i++){
            s[i]=s2[i];
        }
        long ed1=System.currentTimeMillis();
        System.out.println(ed1-st1);

        System.out.println("测试copy循环:");
        long st2=System.currentTimeMillis();
        System.arraycopy(s,1,s,0,s.length-1);
        long ed2=System.currentTimeMillis();
        System.out.println(ed2-st2);

        System.out.println("测试copy循环:");
        st2=System.currentTimeMillis();
        System.arraycopy(s,1,s2,0,s.length-1);
        ed2=System.currentTimeMillis();
        System.out.println(ed2-st2);

        String[] srcArray = new String[1000000];
        String[] forArray = new String[srcArray.length];
        String[] arrayCopyArray  = new String[srcArray.length];

        //初始化数组
        for(int index  = 0 ; index  < srcArray.length ; index ++){
            srcArray[index] = String.valueOf(index);
        }

        long forStartTime = System.currentTimeMillis();
        for(int index  = 0 ; index  < srcArray.length ; index ++){
            forArray[index] = srcArray[index];
        }
        long forEndTime = System.currentTimeMillis();
        System.out.println("for方式复制数组:"  + (forEndTime - forStartTime));

        long arrayCopyStartTime = System.currentTimeMillis();
        System.arraycopy(srcArray,0,arrayCopyArray,0,srcArray.length);
        long arrayCopyEndTime = System.currentTimeMillis();
        System.out.println("System.arraycopy复制数组:"  + (arrayCopyEndTime - arrayCopyStartTime));
    }
我跑了很多次,int数组的效率有时for高,有时arraycopy高。 但是String数组,每次都是arrayCopy高,我在想,是不是arrayCopy在处理引用类型时,只拷贝了其引用导致的?就是都是浅拷贝导致的? https://blog.csdn.net/wangyangzhizhou/article/details/79504818
「已注销」 2019-04-03
  • 打赏
  • 举报
回复
引用 3 楼 LCL_data 的回复:
[quote=引用 2 楼 彩笔梳子 的回复:] [quote=引用 1 楼 NinjaYinJey 的回复:] for --> cpu需要单个计算地址 arraycopy --> cpu计算一个连续的内存地址且一次性创建相同大小的内存
那么请问为啥速度效率上跟for循环在我这实验时更快[/quote] 试验代码贴下?[/quote] 就是删除数组元素时的移动,自我拷贝。
「已注销」 2019-04-03
  • 打赏
  • 举报
回复
引用 3 楼 LCL_data 的回复:
[quote=引用 2 楼 彩笔梳子 的回复:] [quote=引用 1 楼 NinjaYinJey 的回复:] for --> cpu需要单个计算地址 arraycopy --> cpu计算一个连续的内存地址且一次性创建相同大小的内存
那么请问为啥速度效率上跟for循环在我这实验时更快[/quote] 试验代码贴下?[/quote]
 System.out.println("测试for循环:");
        long st1=System.currentTimeMillis();
        for(int i=10000;i<200000000;i++){
            s[i-1]=s[i-1];
        }
System.out.println("测试copy循环:");
        long st2=System.currentTimeMillis();
        System.arraycopy(s,1,s,0,s.length-1);
        long ed2=System.currentTimeMillis();
        System.out.println(ed2-st2);

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧