为什么用余弦函数计算比用正割三角函数计算快得多?
Why is Calculating with the Cosine function so much faster than with the Secant Trig function?
为什么用余弦函数计算比用正割三角函数快很多?
有没有办法加快正割函数的速度?
使用余弦三角函数和正割三角函数计算时,所花费的时间差异很大。
Cosine = finally Done-elapsed time -36.7544sec- or -0.6126mins- or -0.0102hours-
Secant = finally Done-elapsed time -43.2231sec- or -0.7204mins- or -0.0120hours-
下面是我用来测试的代码。我只是取出正割线或余弦线来测试我想要的每条线的速度。
clear all, clc,tic
num_of_values=60000; %number of values to use
a1_dataset =linspace(0,10000,num_of_values)';%
a1_idx = randi (numel (a1_dataset), num_of_values, 1);
a1=a1_dataset (a1_idx);
a2_dataset =linspace(0,.8,num_of_values)';%
a2_idx = randi (numel (a2_dataset), num_of_values, 1);
a2=a2_dataset (a2_idx);
a3_dataset =linspace(-360,360,num_of_values)';%
a3_idx = randi (numel (a3_dataset), num_of_values, 1);
a3=a3_dataset (a3_idx);
array1=[a1,a2,a3];
t_rebuilt=linspace(0,16000,16000);
sig_comb=zeros(1,length(t_rebuilt));
for rr=1:1:length(array1)
sig_comb=sig_comb+array1(rr,2)*cos (((array1(rr,1))*t_rebuilt)+array1(rr, 3)); %test using cosine
%sig_comb=sig_comb+array1(rr,2)*sec (((array1(rr,1))*t_rebuilt)+array1(rr, 3)); %test using secant
end
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);
PS:我正在使用 Octave 3.8.1 Linux
Octave 没有 sec
的原生实现,它使用 cos
实现。查看 edit sec.m
了解详情。
差异是由
造成的
nargin
在 sec
中的函数调用
sec
的函数调用开销
- 除以值
要消除前两个因素,请使用 1./cos(x)
而不是 sec(x)
。要获得完全相同的速度,需要 sec
的本机实现。
为什么用余弦函数计算比用正割三角函数快很多? 有没有办法加快正割函数的速度?
使用余弦三角函数和正割三角函数计算时,所花费的时间差异很大。
Cosine = finally Done-elapsed time -36.7544sec- or -0.6126mins- or -0.0102hours-
Secant = finally Done-elapsed time -43.2231sec- or -0.7204mins- or -0.0120hours-
下面是我用来测试的代码。我只是取出正割线或余弦线来测试我想要的每条线的速度。
clear all, clc,tic
num_of_values=60000; %number of values to use
a1_dataset =linspace(0,10000,num_of_values)';%
a1_idx = randi (numel (a1_dataset), num_of_values, 1);
a1=a1_dataset (a1_idx);
a2_dataset =linspace(0,.8,num_of_values)';%
a2_idx = randi (numel (a2_dataset), num_of_values, 1);
a2=a2_dataset (a2_idx);
a3_dataset =linspace(-360,360,num_of_values)';%
a3_idx = randi (numel (a3_dataset), num_of_values, 1);
a3=a3_dataset (a3_idx);
array1=[a1,a2,a3];
t_rebuilt=linspace(0,16000,16000);
sig_comb=zeros(1,length(t_rebuilt));
for rr=1:1:length(array1)
sig_comb=sig_comb+array1(rr,2)*cos (((array1(rr,1))*t_rebuilt)+array1(rr, 3)); %test using cosine
%sig_comb=sig_comb+array1(rr,2)*sec (((array1(rr,1))*t_rebuilt)+array1(rr, 3)); %test using secant
end
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);
PS:我正在使用 Octave 3.8.1 Linux
Octave 没有 sec
的原生实现,它使用 cos
实现。查看 edit sec.m
了解详情。
差异是由
造成的nargin
在sec
中的函数调用
sec
的函数调用开销
- 除以值
要消除前两个因素,请使用 1./cos(x)
而不是 sec(x)
。要获得完全相同的速度,需要 sec
的本机实现。