- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
为什么在大型复杂数组上运行 std::abs
比使用 sqrt
和 norm
慢大约 8 倍?
#include <ctime>
#include <cmath>
#include <vector>
#include <complex>
#include <iostream>
using namespace std;
int main()
{
typedef complex<double> compd;
vector<compd> arr(2e7);
for (compd& c : arr)
{
c.real(rand());
c.imag(rand());
}
double sm = 0;
clock_t tm = clock();
for (const compd& c : arr)
{
sm += abs(c);
}
cout << sm << ' ' << clock() - tm << endl; // 5.01554e+011 - 1640 ms
sm = 0;
tm = clock();
for (const compd& c : arr)
{
sm += sqrt(norm(c));
}
cout << sm << ' ' << clock() - tm << endl; // 5.01554e+011 - 154
sm = 0;
tm = clock();
for (const compd& c : arr)
{
sm += hypot(c.real(), c.imag());
}
cout << sm << ' ' << clock() - tm << endl; // 5.01554e+011 - 221
}
最佳答案
我认为两者在严格意义上不能被视为相同。
来自 cppreference on std::abs(std::complex)
:
Errors and special cases are handled as if the function is implemented as
std::hypot(std::real(z), std::imag(z))
同样来自 cppreference on std::norm(std::complex)
:
The norm calculated by this function is also known as field norm or absolute square.
The Euclidean norm of a complex number is provided by
std::abs
, which is more costly to compute. In some situations, it may be replaced bystd::norm
, for example, ifabs(z1) > abs(z2)
thennorm(z1) > norm(z2)
.
简而言之,存在从每个函数获得不同结果的情况。其中一些可以在 std::hypot
中找到.注释中还提到了以下内容:
std::hypot(x, y)
is equivalent tostd::abs(std::complex<double>(x,y))
一般来说,结果的准确性可能会有所不同(由于通常的浮点困惑),函数的设计似乎是为了尽可能准确。
关于c++ - std::abs(std::complex) 太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53518381/
我用 cudaMemcpy()一次将 1GB 的数据精确复制到设备。这需要 5.9 秒。反之则需要 5.1 秒。这是正常的吗?函数本身在复制之前有这么多开销吗? 理论上,PCIe 总线的吞吐量至少应为
我正在尝试读取图像的大小并在其高度大于 150 时调整其边距。但是在运行这段代码时,我总是在控制台中得到一个“0”: var coverImg; coverImg =
我正在开发一个 iPhone 应用程序,其中包含一些标准的“相机”功能。保存到相机胶卷真的太慢了,在iPhone 4上大约需要四秒钟。有什么办法可以提高速度吗? 如果您查看默认的 iPhone 相
我创建了一个将图像转换为灰度的类。但它的工作速度太慢了。有没有办法让它运行得更快? 这是我的类(class): @implementation PixelProcessing SYNTHESIZE_S
我使用以下代码,结果是正确的,但 gethostbyaddr 需要大约 30 秒。 function IPAddrToName(IPAddr: string): string; var So
我有以下案例, public class Test { private static final int MAX_NUMBER = 10_00_00; public static vo
我已经正确添加了所有必需的 JARS: Ucanaccess 3.0.4 commons-lang-2.6 commons-logging-1.1.1 hsqldbd jackcess-2.1.3 我
我为特定功能构建了一个多处理密码破解程序(使用单词列表),与使用单个进程相比,它减少了一半的时间。 最初的问题是,它会向您显示破解的密码并终止工作人员,但剩余的工作人员将继续工作,直到他们用完可哈希的
我在我的一个 JSP 中引入了 Sencha 网格。本地 sencha 相当快,但在外部服务器上它太慢了。 我在这里按照部署说明进行操作 http://docs.sencha.com/ext-js/4
我的查询加载时间有很大问题。在这种情况下,我需要 hg_ft_won 列(表:值)中的值,用于 home_team_id 和 away_team_id(表:匹配)。 它确实可以正常工作。加载只需要很长
我现在正在学习不同类型的排序,我发现,从某个点开始,我的快速排序算法根本无法快速工作。 这是我的代码: class QuickSort { // partitioning arr
为什么要模式 [0123]123456|98765 比在 Java 中执行 [0123]123456 然后 98765 慢两倍?所以单独搜索它们比用 OR 执行更快。有人有解释吗? UPD 查看带有结
我有带 Assets 的 Android 应用程序。它们包含 20,000 多个文件,其中大部分是简单的文本或 png 文件,分为不同的文件夹和子文件夹。1 个单个文件的最大大小为 500kb,其中
您好,我在查询中添加了 GROUP_CONCAT 函数,该函数终止了我的查询:/。我的查询是: SELECT u.username,a.user_id,a.id,a.text,a.lang as fr
我正在寻找优化查询的想法。 目前,我有一个 4M 行的表,我只想检索引用的最后 1000 行: SELECT * FROM customers_material_events WHERE refere
我在我的应用程序中使用 NSURLConnection,我在其中扫描条形码,通过 NSURLConnection 发送 XML,Java 服务向我发回 XML。我的问题是,使用 Wifi 时,响应时间
当我运行以下程序时,执行大约需要 7 到 8 分钟。我真的不确定我哪里弄错了,因为这个程序执行起来要花很多时间。 public class Test { public stat
我正在使用 NSFetchResultsController 从数据库中接收项目(有 80.000 个项目)。 这是我的谓词:@"(desc CONTAINS[cd] %@)", [any text]
我在 x_data 中有一个 3x2000 numpy 数组,在 y_data 中有一个 1x2000 numpy 数组,我将其传递给此函数 regress 以给我一条回归线。它工作正常。问题是我正在
我正在做一个项目,我需要改变图像的亮度和对比度,它是亮度而不是亮度。所以我一开始的代码是 for (int y = 0; y (y, x); // read pixel (0,0)
我是一名优秀的程序员,十分优秀!