在Matlab中提高函数调用的速度

Glo*_*ria 3 performance matlab function call

我有一个带调用函数的代码,它给出了系统中每个基因的类型.我可以通过比较每个基因与其子女和父母的顺序来找到它.代码工作正常,有一小部分单元格阵列,但当我将数量增加到数千时,需要几个小时.代码是:

Types=[];
type1=level1_root; % it is fixed value (GO:0008150)
% sample values for p1 and c1 are given below
for k=1:100
    type{k}=type_fc(p1,c1,type1); % a function call - see function below
    type1=type{k}'; %'
    temp1=num2cell(repmat(k+1,length(type1),1));
    type1=[type1 temp1];
    Types=[Types; type1];
 end
 % display the output:
 Types
Run Code Online (Sandbox Code Playgroud)

子功能:

function type=type_fc(p1,c1,type1)
type=[];
for j=1:length(type1)
    for i=1:length(p1)
        a=[p1(i),c1(i)];
        if isequal(a(1), type1(j))
            type=[type a(2)];
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

对于13个基因,我有这些样本输入:

p1'= %refer to parent genes  
      'GO:0008150'
      'GO:0016740'
      'GO:0016787'
      'GO:0008150'
      'GO:0016740'
      'GO:0016740'
      'GO:0016787'
      'GO:0016787'
      'GO:0016787'
      'GO:0006810'
      'GO:0006412'
      'GO:0004672'


 c1'=  % refer to children genes    
  'GO:0016740'
  'GO:0016787'
  'GO:0006810'
  'GO:0006412'
  'GO:0004672'
  'GO:0016779'
  'GO:0004386'
  'GO:0003774'
  'GO:0016298'
  'GO:0016192'
  'GO:0005215'
  'GO:0030533'
Run Code Online (Sandbox Code Playgroud)

结果将是: Types =

  'GO:0016740'    [2]
  'GO:0006412'    [2]
  'GO:0016787'    [3]
  'GO:0004672'    [3]
  'GO:0016779'    [3]
  'GO:0005215'    [3]
  'GO:0006810'    [4]
  'GO:0004386'    [4]
  'GO:0003774'    [4]
  'GO:0016298'    [4]
  'GO:0030533'    [4]
  'GO:0016192'    [5]
Run Code Online (Sandbox Code Playgroud)

你知道如何提高这段代码的速度吗?

Eit*_*n T 7

乍一看,我可以在您的代码中发现一些问题:

  1. 首先,Types并且type在循环内动态增长.就执行时间而言,这在MATLAB中可能非常昂贵.相反,在循环之前预先分配内存(即,使用预定的最终元素数创建数组),您可能会看到性能的显着提高.

  2. 你正在使用循环.如果有一个矢量化解决方案(我还没有检查过),计算时间可能会少得多.

  3. 您使用ij索引作为循环迭代器的变量名称.这些变量已经有另一个目的:它们代表虚构的单位sqrt(-1).MATLAB仍然允许使用ij用于变量名称,但它为了找出正确的上下文而执行的变量名称解析确实具有较小的成本.你还是挑其他的名字,甚至iijj.
    编辑:同样的type,它已经是MATLAB中的保留函数名称.

尝试以下优化版本,它应该运行至少一个数量级:

Types = cell(numel(c1), 2);      % # Preallocate memory
type1 = level1_root;             % # ... or p1{1}
kk = [1, 2];                     % # Initialize indices
while ~isempty(type1)
    type_fc = cellfun(@(x)c1(strcmp(x, p1)), type1, 'Uniform', false);
    type1 = vertcat(type_fc{:});
    idx = kk(1):kk(1) + numel(type1) - 1;
    Types(idx, 1) = type1;
    Types(idx, 2) = {kk(2)};
    kk = kk + [numel(type1), 1]; % # Advance indices
end
Types = Types(1:kk(1) - 1, :);   % # Remove empty output cells
Run Code Online (Sandbox Code Playgroud)