相关疑难解决方法(0)

Branchless K-means (or other optimizations)

Note: I'd appreciate more of a guide to how to approach and come up with these kinds of solutions rather than the solution itself.

I have a very performance-critical function in my system showing up as a number one profiling hotspot in specific contexts. It's in the middle of a k-means iteration (already multi-threaded using a parallel for processing sub-ranges of points in each worker thread).

ClusterPoint& pt = points[j];
pt.min_index = -1;
pt.min_dist = numeric_limits<float>::max();
for (int i=0; i …
Run Code Online (Sandbox Code Playgroud)

c++ optimization performance

59
推荐指数
5
解决办法
2351
查看次数

剪辑数字最有效/优雅的方式?

给定一个实数(n),这个实数可以是(上)的最大值,这个实数可以是(更低)的最小值,我们怎样才能最有效地剪切n,使它保持在低位和高位之间?

当然,使用一堆if语句可以做到这一点,但那很无聊!更紧凑,优雅/有趣的解决方案呢?

我自己的快速尝试(C/C++):

float clip( float n, float lower, float upper )
{
    n = ( n > lower ) * n + !( n > lower ) * lower;
    return ( n < upper ) * n + !( n < upper ) * upper;
}
Run Code Online (Sandbox Code Playgroud)

我确信还有其他更好的方法可以做到这一点,这就是为什么我把它放在那里......!

c++ algorithm math logic

47
推荐指数
7
解决办法
6万
查看次数

灰度至红绿蓝(MATLAB Jet)色标

给我一个基本上是图像的数据集,但是图像中的每个像素都表示为从-1到1的值.我正在编写一个需要采用这些-1到1灰度值的应用程序,并将它们映射到MATLAB"Jet"色标(红 - 绿 - 蓝色渐变)的相关RGB值.

我很好奇,如果有人知道如何取一个线性值(如-1到1)并将其映射到此比例.请注意,我实际上并没有使用MATLAB(我也不能),我只需要获取灰度值并将其放在Jet渐变上.

谢谢,亚当

c++ matlab colors

34
推荐指数
3
解决办法
4万
查看次数

如何强制数字在C#范围内?

在C#中,我经常要将整数值限制为一系列值.例如,如果应用程序需要百分比,则用户输入中的整数不得小于零或大于一百.另一个例子:如果有五个网页可以访问Request.Params["p"],我希望值为1到5,而不是0或256或99999.

我常常写一个非常丑陋的代码,如:

page = Math.Max(0, Math.Min(2, page));
Run Code Online (Sandbox Code Playgroud)

甚至是丑陋的:

percentage =
    (inputPercentage < 0 || inputPercentage > 100) ?
    0 :
    inputPercentage;
Run Code Online (Sandbox Code Playgroud)

在.NET Framework中没有更智能的方法来做这些事情吗?

我知道我可以编写一个通用的方法int LimitToRange(int value, int inclusiveMinimum, int inlusiveMaximum)并在每个项目中使用它,但也许框架中已经有一个神奇的方法了?

如果我需要手动执行,那么在第一个示例中执行我正在执行的操作的"最佳"(即,更简单,更快速)的方法是什么?像这样的东西?

public int LimitToRange(int value, int inclusiveMinimum, int inlusiveMaximum)
{
    if (value >= inclusiveMinimum)
    {
        if (value <= inlusiveMaximum)
        {
            return value;
        }

        return inlusiveMaximum;
    }

    return inclusiveMinimum;
}
Run Code Online (Sandbox Code Playgroud)

.net c# math performance

31
推荐指数
5
解决办法
5万
查看次数

限制struct成员的值

我想创建一个存储颜色RGB值的简单结构.r,g和b应该是[0,1]中的双数.

struct Color 
{
  Color(double x): r{x}, g{x}, b{x} {
    if (r < 0.0) r = 0.0;
    if (r > 1.0) r = 1.0;
    if (g < 0.0) g = 0.0;
    if (g > 1.0) g = 1.0;
    if (b < 0.0) b = 0.0;
    if (b > 1.0) b = 1.0;
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有比使用if语句更好的方法?

c++ c++11

3
推荐指数
2
解决办法
718
查看次数

标签 统计

c++ ×4

math ×2

performance ×2

.net ×1

algorithm ×1

c# ×1

c++11 ×1

colors ×1

logic ×1

matlab ×1

optimization ×1