`
aigo
  • 浏览: 2568975 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

[C++11]std::atomic、boost::atomic、Interlocked三者的性能比较(benchmark)

阅读更多

原文作者:@玄冬Wong

好久没做过benchmark了,这次之所以想测试下,是怕std::atomic的效率没有windows的Interlocked性能好,测一下发现,性能差不多,Interlocked微弱的领先优势可以忽略不计。

先公布结果:三者的性能几乎相同,windows的Interlocked略好一点点

 

测试代码:

#ifdef _WIN64
#ifndef _DEBUG
#pragma comment(lib, "libboost_atomic-vc140-mt-1_60.lib")
#endif
#endif

//#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <atomic>
#include <boost/atomic.hpp> 
#include <time.h>  
#include <thread>
#include <list> 


#define MAX_THREADS 16  
#define LOOP_COUNT 10000000

volatile long g_CountWin = 0;
std::atomic<long> g_CountStd = 0;
boost::atomic_long g_CountBoost(0);

void Interlocked_fun()
{
	for (int i = 0; i < LOOP_COUNT; i++)
	{
		InterlockedIncrement((LPLONG)&g_CountWin);
	}
}

void std_atomic_fun()
{
	for (int i = 0; i < LOOP_COUNT; i++)
	{
		++g_CountStd;
	}
}

void boost_atomic_fun()
{
	for (int i = 0; i < LOOP_COUNT; i++)
	{
		++g_CountBoost;
	}
}



void test_Interlocked()
{
	std::list<std::thread*> threadlist;

	//测试Interlocked
	printf("testing Interlocked...\n");
	clock_t start = clock();
	for (int i = 0; i < MAX_THREADS; ++i)
	{
		std::thread *t1 = new std::thread((&Interlocked_fun));
		threadlist.push_back(t1);
	}
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		(*i)->join();
	}
	clock_t finish = clock();
	printf("result:%d\n", g_CountWin);
	printf("cost:%dms\n", finish - start);
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		delete(*i);
	}
}

void test_std_atomic()
{
	std::list<std::thread*> threadlist;

	//测试std::atomic
	printf("testing std::atomic...\n");
	clock_t start = clock();
	for (int i = 0; i < MAX_THREADS; ++i)
	{
		std::thread *t1 = new std::thread((&std_atomic_fun));
		threadlist.push_back(t1);
	}
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		(*i)->join();
	}
	clock_t finish = clock();
	printf("result:%d\n", g_CountStd);
	printf("cost:%dms\n", finish - start);
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		delete(*i);
	}
}

void test_boost_atomic()
{
	std::list<std::thread*> threadlist;

	//测试boost::atomic
	printf("testing boost::atomic...\n");
	clock_t start = clock();
	for (int i = 0; i < MAX_THREADS; ++i)
	{
		std::thread *t1 = new std::thread((&boost_atomic_fun));
		threadlist.push_back(t1);
	}
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		(*i)->join();
	}
	clock_t finish = clock();
	printf("result:%d\n", g_CountBoost);
	printf("cost:%dms\n", finish - start);
	for (std::list<std::thread*>::const_iterator i = threadlist.begin(); i != threadlist.end(); i++)
	{
		delete(*i);
	}
}

int main(char* args, int size)
{
	test_Interlocked();
	//test_std_atomic();
	//test_boost_atomic();
}

 

 

三种API的测试线程数都是16个并发线程,测试输出结果如下(跑了5次,取的平均值):

testing Interlocked...

result:160000000

cost:4926ms

 

testing std::atomic...

result:160000000

cost:4952ms

 

testing boost::atomic...

result:160000000 

cost:4949ms

 

测试环境:

boost 1.60

windows 10 pro x64

VS2015企业版 update2,release x64

CPU:i7二代移动版

 

 

分享到:
评论

相关推荐

    安全栈表实现,C++11实现,使用atomic特性

    在实际开发中,为了提高性能,还可以考虑使用`std::atomic_flag`来实现自旋锁,或者使用`std::mutex`等同步原语来保护非原子操作的部分。此外,无锁数据结构(如无锁栈)虽然能提供更好的并发性能,但实现起来更为...

    C++ 11 开发中的 Atomic 原子操作.rar_C++11、原子_atomic c++_c++ atomic使用_c++

    下面将详细介绍C++11中`std::atomic`的相关知识点。 1. **原子类型(Atomic Types)** C++11定义了一个`std::atomic`模板类,用于创建原子类型变量。这些变量在多线程环境中可以保证其基本操作(如读取、写入或...

    C++高效编程:内存与性能优化

    10. **并行和并发编程**:C++11引入了多线程支持,如`std::thread`和`std::async`,以及原子操作(`std::atomic`),允许开发者利用多核处理器进行并行计算,提高性能。 11. **编译器优化选项**:使用编译器的优化...

    基于C++11实现的定时器

    C++11引入了许多新特性,如线程库(std::thread)、原子操作(std::atomic)以及更好的时间处理功能,这些都使得实现定时器变得更加便捷。在这个基于C++11的定时器实现中,我们主要会关注以下几个关键知识点: 1. *...

    每天学点C++(C++实例教程:教程+源码)thread.zip

    C++提供了多种同步原语,如`std::mutex`用于互斥锁,`std::condition_variable`用于条件变量,以及`std::atomic`类型用于原子操作。这些工具可以帮助确保在多线程环境中数据的一致性和完整性。例如,使用`std::mutex...

    深入理解C++11:C++11新特性解析与应用源代码

    《深入理解C++11:C++11新特性解析与应用源代码》是一本专为C++开发者设计的书籍,旨在帮助读者掌握C++11标准中的新特性和最佳实践。C++11是C++语言的一个重大更新,引入了许多重要的改进,包括语法上的优化、性能...

    C++11_14高级编程 Boost程序库探秘, 3rd,深入理解C++11,Effective+C,并发

    - **Boost.Thread**:在C++11标准之前,Boost库就提供了线程支持,现在与C++11的`std::thread`相兼容。 - **Boost.Asio**:异步I/O库,支持网络通信和定时任务。 - **Boost.Bind**和**Boost.Lambda**:函数对象绑定...

    每天学点C++(C++实例教程:教程+源码)多线程.zip

    C++11引入了线程安全的容器,如`std::atomic`,它们在多线程环境下提供原子操作,保证数据的一致性。 通过学习这个实例教程,你将能掌握C++中的基本多线程编程技术,并了解如何在实际项目中有效地利用多线程提升...

    C++11并发编程实战源代码

    `std::atomic`类模板提供了一种方式来确保对变量的操作是原子的,避免数据竞争。C++11的内存模型定义了不同线程之间的数据依赖关系,确保正确同步和可见性。 五、future和promise `std::future`和`std::promise`是...

    hashtable C++无锁(std_atomic)&U32非0&不可扩大.rar

    无锁哈希表(Lock-free Hash Table)是一种高级并发数据结构,它利用原子操作(std::atomic)来实现线程安全的数据访问,避免了在多线程环境下使用锁导致的性能瓶颈。在这个“hashtable C++无锁(std_atomic)&U32非0&...

    C++ string类的隐式共享写时拷贝实现代码

    8. **性能优化**:在实现时,可以考虑使用`std::atomic_flag`代替`std::atomic&lt;int&gt;`作为引用计数,因为前者通常具有更好的性能。此外,可以使用锁或其他同步机制来控制对字符数组的修改,以确保并发安全性。 通过...

    C++17 - The Complete Guide.pdf

    * std::atomic:C++17引入了std::atomic类,允许开发者实现原子操作。 错误处理 C++17引入了许多新的错误处理机制,包括: * std::error_code:C++17引入了std::error_code类,允许开发者表示错误代码。 * std::...

    Effective.Modern.C++-42.Specific.Ways.to.Improve.Your.Use.of.C++11.and.C++14

    书中比较了std::atomic和volatile的差异,并解释了它们与C++并发API的关系。 8. **现代C++编程中的最佳实践修订**:C++11和C++14引入的新特性要求程序员在编写现代C++代码时对一些传统的最佳实践进行重新评估。本书...

    C++11StdLib Table of Code Examples

    还有同步机制如互斥量`std::mutex`、条件变量`std::condition_variable`,以及原子操作`std::atomic`,用于线程间的通信和同步。 2. **算法(Algo)** `algo`目录下的代码展示了C++11对算法库的增强,如`std::sort...

    C++11 Multithread

    `std::atomic&lt;T&gt;`模板类允许在不使用锁的情况下执行原子操作,这对于实现高效的并发算法非常有用。 ### 四、总结 通过本文,我们深入了解了C++11多线程库的各个方面,包括基本的线程管理、同步机制以及一些高级...

    C++并发编程实战:示例源源码

    3. **原子操作与内存模型**:C++11引入了`std::atomic`库,提供了一组原子类型和操作,确保在多线程环境中这些操作不会被中断。内存模型定义了如何处理不同线程间的内存访问顺序,确保正确的行为。 4. ** futures ...

    C++11 多线程

    `std::atomic`类型提供了强大的原子操作能力,如比较交换、加法等。 #### 六、基于锁的并发数据结构设计 - **锁的使用**:设计基于锁的数据结构时,需要考虑何时以及如何使用锁。例如,可以通过在关键部分加锁来...

    C++11标准库比较全面.rar

    《C++11标准库比较全面.rar》是一个包含有C++编程语言相关资源的压缩文件,特别是关于C++11标准库的详细信息。C++11是C++语言的一个重要版本,它引入了大量的新特性、优化和改进,极大地提升了C++的现代性和实用性。...

Global site tag (gtag.js) - Google Analytics