数学建模社区-数学中国

标题: for循环——有趣例子 [打印本页]

作者: 森之张卫东    时间: 2015-9-7 22:47
标题: for循环——有趣例子

4.5
比较向量算法和循环为了比较循环和向量算法执行若无事所用的时间,用两种方法编程并测试三个运算所花的时间。

1.for循环计算110000的之间每一个整数的平方,而事先不初始化平方数组。

2.for循环计算110000的之间每一个整数的平方,而事先初始化平方数组。

3.用向量算法计算计算110000的之间每一个整数的平方。

答案:

这个程序必须用上面提供的三种方示计算出110000之间的每一个整数的平方,并测试每一个种算法的时间。测试时间要用到matlab函数tictoctic函数复位内建计时器,而toc函数则从最后一次调用tic以秒开始计时。

因为在许多的计算机中它的时间钟是相当粗略的,所以有必要多运行几次以获得相应的平均数。

下面就是用三种方法编出的matlab

程序。


  1. % Script file: timings.m
  2. %
  3. % Purpose:
  4. % This program calculates the time required to
  5. % calculate the squares of all integers from 1 to
  6. % 10,000 in three different ways:
  7. % 1. Using a for loop with an uninitialized output
  8. % array.
  9. % 2. Using a for loop with an preallocated output
  10. % array.
  11. % 3. Using vectors.
  12. %
  13. % Record of revisions:
  14. % Date Programmer Description of change
  15. % ==== ========== =====================
  16. % 12/08/97 S. J. Chapman Original code
  17. %
  18. % Define variables:
  19. % ii, jj Loop index
  20. % average1 Average time for calculation 1
  21. % average2 Average time for calculation 2
  22. % average3 Average time for calculation 3
  23. % maxcount Number of times to loop calculation
  24. % square Array of squares
  25. % leap_day Extra day for leap year
  26. % month Month(mm)
  27. % year Year(yyyy)
  28. % Perform calculation with an uninitialized array
  29. % "square". This calculation is done only once
  30. % because it is so slow.
  31. maxcount = 1; % One repetition
  32. tic; % Start timer
  33. for jj = 1:maxcount
  34.     clear square % Clear output array
  35.     for ii = 1:10000
  36.         square(ii) = ii^2; % Calculate square
  37.     end
  38. end
  39. average1 = (toc)/maxcount; % Calculate average time
  40. % Perform calculation with a preallocated array
  41. % "square". This calculation is averaged over 10
  42. % loops.
  43. maxcount = 10; % One repetition
  44. tic; % Start timer
  45. for jj = 1:maxcount
  46.     clear square % Clear output array
  47.     square = zeros(1,10000); % Preinitialize array
  48.     for ii = 1:10000
  49.         square(ii) = ii^2; % Calculate square
  50.     end
  51. end
  52. average2 = (toc)/maxcount; % Calculate average time
  53. % Perform calculation with vectors. This calculation
  54. % averaged over 100 executions.
  55. maxcount = 100; % One repetition
  56. tic; % Start timer
  57. for jj = 1:maxcount
  58.     clear square % Clear output array
  59.     ii = 1:10000; % Set up vector
  60.     square = ii.^2; % Calculate square
  61. end
  62. average3 = (toc)/maxcount; % Calculate average time
  63. % Display results
  64. fprintf('Loop / uninitialized array = %8.4f\n', average1);
  65. fprintf('Loop / initialized array = %8.4f\n', average2);
  66. fprintf('Vectorized = %8.4f\n', average3);
复制代码





作者: 森之张卫东    时间: 2015-9-7 22:48
代码格式优美,大家共同学习吧!!!

作者: peter凯    时间: 2015-9-8 09:15
不错有哦有哟与哦有哦

作者: peter凯    时间: 2015-9-8 09:16
这个真不错得得





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5