C ++ Exp与日志:哪个更快? [英] C++ Exp vs. Log: Which is faster?

查看:95
本文介绍了C ++ Exp与日志:哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++应用程序,需要比较两个值并确定哪个更大.唯一的麻烦是,一个数字表示在对数空间中,而另一个数字则没有.例如:

I have a C++ application in which I need to compare two values and decide which is greater. The only complication is that one number is represented in log-space, the other is not. For example:

double log_num_1 = log(1.23);
double num_2 = 1.24;

如果我想比较num_1num_2,我必须使用log()exp(),我想知道一个计算是否比另一个计算更容易(例如,运行时间更短,一般来说).您可以假设我正在使用标准的cmath库.

If I want to compare num_1 and num_2, I have to use either log() or exp(), and I'm wondering if one is easier to compute than the other (i.e. runs in less time, in general). You can assume I'm using the standard cmath library.

换句话说,以下内容在语义上是等效的,因此速度更快:

In other words, the following are semantically equivalent, so which is faster:

if(exp(log_num_1) > num_2)) cout << "num_1 is greater";

if(log_num_1 > log(num_2)) cout << "num_1 is greater";

推荐答案

AFAIK的算法,复杂度是相同的,不同之处应该只是一个(希望可以忽略不计)常数. 因此,我会使用exp(a) > b,只是因为它不会在无效输入时中断.

AFAIK the algorithms, the complexity is the same, the difference should be only a (hopefully negligible) constant. Due to this, I'd use the exp(a) > b, simply because it doesn't break on invalid input.

这篇关于C ++ Exp与日志:哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆