找回密码
 注册
Simdroid-非首页
查看: 62|回复: 3

关于变量清零后的耗时问题,谁能解释一下原因?

[复制链接]
发表于 2009-2-24 11:10:39 | 显示全部楼层 |阅读模式 来自 辽宁大连
MATLAB 2008b下,

  1. tic
  2. for m = 1:1000000
  3.     a.sinx = sin(m);
  4. end;
  5. toc;
复制代码

运行时间约为0.16 s
但当改为

  1. tic
  2. a = [];
  3. for m = 1:1000000
  4.     a.sinx = sin(m);
  5. end;
  6. toc;
复制代码

运算时间为6秒
为什么在循环体外加一句a=[],耗时就这么严重?(profile viewer表明耗时全在循环体内部)

评分

1

查看全部评分

发表于 2009-2-24 11:32:09 | 显示全部楼层 来自 北京
Simdroid开发平台
我用2007a
前者耗时1.199597秒
后者耗时1.198657秒
没有多大的差别

看来2008b确实有不同啊
是什么原因呢?是mupad的问题吗?
我也想知道
回复 不支持

使用道具 举报

发表于 2009-2-24 13:14:22 | 显示全部楼层 来自 陕西宝鸡
我用 7.0.1
测试了几次
第一个代码:Elapsed time is 2.003000 seconds.
第二个代码:Elapsed time is 2.003000 seconds.
:victory:
回复 不支持

使用道具 举报

发表于 2009-2-24 13:18:01 | 显示全部楼层 来自 黑龙江哈尔滨
我用2007a
前者耗时1.199597秒
后者耗时1.198657秒
没有多大的差别

看来2008b确实有不同啊
是什么原因呢?是mupad的问题吗?
我也想知道
ljelly 发表于 2009-2-24 11:32

这应该不是2008b的问题,也应该与mupad无关,我用matlab2008a试了试
  1. tic
  2. for m = 1:1000000
  3. a.sinx = sin(m);
  4. end;
  5. toc;
  6. Elapsed time is 0.163418 seconds.
复制代码
  1. tic
  2. a = [];
  3. for m = 1:1000000
  4. a.sinx = sin(m);
  5. end;
  6. toc;
  7. Elapsed time is 4.416226 seconds.
复制代码

但是我把a=[]变为a=1时
  1. tic
  2. a = 1;
  3. for m = 1:1000000
  4. a.sinx = sin(m);
  5. end;
  6. toc;
  7. ??? Warning: Struct field assignment overwrites a value with class "double".
  8. See MATLAB 7.0.4 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning for details.
  9. Elapsed time is 4.568766 seconds
复制代码

可以看出它是7.0.4后出现的问题,我用matlab7.0试了试
  1. tic
  2. for m = 1:1000000
  3. a.sinx = sin(m);
  4. end;
  5. toc;
  6. Elapsed time is 2.516000 seconds.
  7. >> tic
  8. a = [];
  9. for m = 1:1000000
  10. a.sinx = sin(m);
  11. end;
  12. toc;
  13. Elapsed time is 2.531000 seconds.
  14. >> tic
  15. a = 1;
  16. for m = 1:1000000
  17. a.sinx = sin(m);
  18. end;
  19. toc;
  20. Elapsed time is 2.516000 seconds.
复制代码

这只能说明7.04之后对循环的加速功能更好了,

评分

1

查看全部评分

回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-9-20 10:37 , Processed in 0.040775 second(s), 17 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表