草庐IT

c++ - "if"语句对性能有多大影响?

coder 2024-02-10 原文

有一些不同大小的 IPTables(例如 255 或 16384 或 512000!!)。每个表的每个条目都包含一个唯一的 IP 地址(十六进制格式)和一些其他值。 IP总数为800万。 所有IPTables的所有IP都排序

我们需要每秒搜索 IPTable 300,000 次。我们目前查找 IP 的算法如下:

// 10 <number of IPTables <20
//_rangeCount = number of IPTables 
s_EntryItem* searchIPTable(const uint32_t & ip) {
        for (int i = 0; i < _rangeCount; i++) {
            if (ip > _ipTable[i].start && ip < _ipTable[i].end) {
                int index = ip - _ipTable[i].start;
                    return (_ipTable[i].p_entry + index);
                }
            }
            return NULL;
        }

可以看出,在最坏的情况下,给定IP地址的比较次数为_rangeCount *2,“if”语句检查的次数为_rangeCount。

假设我想更改 searchIPTable 并使用更有效的方法在 IPTables 中查找 IP 地址。据我所知,对于排序数组,二进制搜索等著名搜索算法的最佳软件实现需要 log(n) 比较(在最坏的情况下)。

因此,查找 IP 地址的比较次数为 log(8000000),等于 ~23。

问题一:

正如蜜蜂所见,两种算法(_rangeCount vs 23)所需的比较次数之间存在一点差距,但在第一种方法中,有一些“if”语句可能会影响性能。如果你想运行第一个算法 10 次,显然第一个算法有更好的性能,但我知道运行两个算法 3000,000 次的想法!你的想法是什么?

问题二:

是否有更高效的搜索IP的算法或解决方案?

最佳答案

好奇心被激起,我写了一个测试程序(如下)并在我的 macbook 上运行。

这表明基于 std::unordered_map(查找时间 == 常数时间)的天真解决方案能够每秒搜索 560 万次具有 800 万个条目的 ip4 地址表。

这很容易超出要求。

更新:为了回应我的批评,我已将测试空间增加到所需的 8m ip 地址。我还将测试规模增加到 1 亿次搜索,其中 20% 会是命中率。

通过如此大的测试,我们可以清楚地看到与有序映射(对数时间查找)相比,使用 unordered_map 的性能优势。

所有测试参数都是可配置的。

#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <random>
#include <tuple>
#include <iomanip>
#include <utility>

namespace detail
{
    template<class T>
    struct has_reserve
    {
        template<class U> static auto test(U*p) -> decltype(p->reserve(std::declval<std::size_t>()), void(), std::true_type());
        template<class U> static auto test(...) -> decltype(std::false_type());

        using type = decltype(test<T>((T*)0));
    };
}

template<class T>
using has_reserve = typename detail::has_reserve<T>::type;


using namespace std::literals;

struct data_associated_with_ip {};
using ip_address = std::uint32_t;

using candidate_vector = std::vector<ip_address>;

static constexpr std::size_t search_space_size = 8'000'000;
static constexpr std::size_t size_of_test = 100'000'000;

std::vector<ip_address> make_random_ip_set(std::size_t size)
{
    std::unordered_set<ip_address> results;
    results.reserve(size);

    std::random_device rd;
    std::default_random_engine eng(rd());
    auto dist = std::uniform_int_distribution<ip_address>(0, 0xffffffff);
    while (results.size() < size)
    {
        auto candidate = dist(eng);
        results.emplace(candidate);
    }

    return { std::begin(results), std::end(results) };
}

template<class T, std::enable_if_t<not has_reserve<T>::value> * = nullptr>
void maybe_reserve(T& container, std::size_t size)
{
    // nop
}

template<class T, std::enable_if_t<has_reserve<T>::value> * = nullptr>
decltype(auto) maybe_reserve(T& container, std::size_t size)
{
    return container.reserve(size);
}

template<class MapType>
void build_ip_map(MapType& result, candidate_vector const& chosen)
{
    maybe_reserve(result, chosen.size());
    result.clear();

    for (auto& ip : chosen)
    {
        result.emplace(ip, data_associated_with_ip{});
    }
}

// build a vector of candidates to try against our map
// some percentage of the time we will select a candidate that we know is in the map
candidate_vector build_candidates(candidate_vector const& known)
{
    std::random_device rd;
    std::default_random_engine eng(rd());
    auto ip_dist = std::uniform_int_distribution<ip_address>(0, 0xffffffff);
    auto select_known = std::uniform_int_distribution<std::size_t>(0, known.size() - 1);
    auto chance = std::uniform_real_distribution<double>(0, 1);
    static constexpr double probability_of_hit = 0.2;

    candidate_vector result;
    result.reserve(size_of_test);
    std::generate_n(std::back_inserter(result), size_of_test, [&]
                    {
                        if (chance(eng) < probability_of_hit)
                        {
                            return known[select_known(eng)];
                        }
                        else
                        {
                            return ip_dist(eng);
                        }
                    });

    return result;
}


int main()
{

    candidate_vector known_candidates = make_random_ip_set(search_space_size);
    candidate_vector random_candidates = build_candidates(known_candidates);


    auto run_test = [&known_candidates, &random_candidates]
    (auto const& search_space)
    {

        std::size_t hits = 0;
        auto start_time = std::chrono::high_resolution_clock::now();
        for (auto& candidate : random_candidates)
        {
            auto ifind = search_space.find(candidate);
            if (ifind != std::end(search_space))
            {
                ++hits;
            }
        }
        auto stop_time = std::chrono::high_resolution_clock::now();
        using fns = std::chrono::duration<long double, std::chrono::nanoseconds::period>;
        using fs = std::chrono::duration<long double, std::chrono::seconds::period>;
        auto interval = fns(stop_time - start_time);
        auto time_per_hit = interval / random_candidates.size();
        auto hits_per_sec = fs(1.0) / time_per_hit;

        std::cout << "ip addresses in table: " << search_space.size() << std::endl;
        std::cout << "ip addresses searched: " << random_candidates.size() << std::endl;
        std::cout << "total search hits    : " << hits << std::endl;
        std::cout << "searches per second  : " << std::fixed << hits_per_sec << std::endl;
    };

    {
        std::cout << "building unordered map:" << std::endl;
        std::unordered_map<ip_address, data_associated_with_ip> um;
        build_ip_map(um, known_candidates);
        std::cout << "testing with unordered map:" << std::endl;
        run_test(um);
    }

    {
        std::cout << "\nbuilding ordered map  :" << std::endl;
        std::map<ip_address, data_associated_with_ip> m;
        build_ip_map(m, known_candidates);
        std::cout << "testing with ordered map  :" << std::endl;
        run_test(m);
    }

}

示例结果:

building unordered map:
testing with unordered map:
ip addresses in table: 8000000
ip addresses searched: 100000000
total search hits    : 21681856
searches per second  : 5602458.505577

building ordered map  :
testing with ordered map  :
ip addresses in table: 8000000
ip addresses searched: 100000000
total search hits    : 21681856
searches per second  : 836123.513710

测试条件:

MacBook Pro (Retina, 15-inch, Mid 2015)
Processor: 2.2 GHz Intel Core i7
Memory: 16 GB 1600 MHz DDR3
Release build (-O2)

使用主电源运行。

关于c++ - "if"语句对性能有多大影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39054847/

有关c++ - "if"语句对性能有多大影响?的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  4. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  5. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  6. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  9. ruby-on-rails - `a ||= b` 和 `a = b if a.nil 之间的区别? - 2

    我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行

  10. ruby - ruby 中有 each_if 吗? - 2

    假设我在Ruby中有这个each循环。@list.each{|i|putsiifi>10breakend}我想循环遍历列表直到满足条件。这让我感到“不像Ruby”,因为我是Ruby的新手,是否有Ruby方法可以做到这一点? 最佳答案 您可以使用Enumerable#detect或Enumerable#take_while,取决于您想要的结果。@list.detect{|i|putsii>10}#Returnsthefirstelementgreaterthan10,ornil.正如其他人所指出的,更好的风格是先进行子选择,然后再对其

随机推荐