我一直听说矢量化代码比MATLAB中的循环运行得更快.但是,当我尝试向量化我的MATLAB代码时,它似乎运行得更慢.
我用tic和toc衡量时间.我只更改了程序中单个函数的实现.我的矢量化版本在47.228801几秒钟内运行,我的for-loop版本在16.962089几秒钟内运行.
同样在我的主程序中,我使用大数字表示N,N = 1000000并且DataSet的大小是1 301,并且我为具有相同大小和N的不同数据集运行了多个版本.
为什么矢量化的速度要慢得多,如何才能进一步提高速度呢?
"矢量化"版本
function [RNGSet] = RNGAnal(N,DataSet)
%Creates a random number generated set of numbers to check accuracy overall
% This function will produce random numbers and normalize a new Data set
% that is derived from an old data set by multiply random numbers and
% then dividing by N/2
randData = randint(N,length(DataSet));
tempData = repmat(DataSet,N,1);
RNGSet = randData .* tempData;
RNGSet …Run Code Online (Sandbox Code Playgroud)