惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
B
Blog RSS Feed
H
Help Net Security
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
L
LangChain Blog
Vercel News
Vercel News

博客园 - woaiusd

claude各种客户端的关系 pytorch对大模型推理,实现简单的对话 Agent原理与模拟 为红米Note 5 Pro编译Lineage OS 15.1的各种坑 C语言调用DIRECT3D的实例代码,通过lpVtbl字段进行 OBS插件学习入门:一个非常简单的、调节音量的filter 从工程角度看C++观察者模式中的接口是否需要提供默认的实现 Windows音频SDK的发展历程 闲话__stdcall, __cdecl, __fastcall出现的历史背景以及各自解决的问题 为什么需要超出48K的音频采样率,以及PCM到DSD的演进 wireshark使用笔记 java.lang.NullPointerException Ubuntu中QT使用FFmpeg的奇怪问题 QT开启C++11的支持 尝鲜CodeBlocks ops中set_sysclk set_clkdiv set_pll详解 在内核中异步请求设备固件firmware的测试代码 探究MaxxBass音效 Alsa驱动snd_soc_read的底层实现 Device Drivers Should Not Do Power Management 探究platform_driver中“多态”思想 探究platform_driver中的shutdown用途
managed_shared_memory.construct造成的性能损失
woaiusd · 2015-10-08 · via 博客园 - woaiusd

boost中的IPC进程间通信非常好用,可以直接在共享内存上创建对象,相当于new分配器,实测发现它的分配算法还是有点耗时。第一个测试代码仅仅分配一次,然后频繁的复制,每秒钟可以复制4200次左右。

// HelloBoostIPC.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <cstdlib> //std::system
#include <cstddef>
#include <cassert>
#include <utility>
#include <string>
#include <Windows.h>

using namespace std;
using namespace boost;
using namespace boost::interprocess;  

char dummy[1280*720*3];

class VideoFrame
{
public:
	int width;
	int height;
	boost::interprocess::interprocess_mutex mutex;
	char data[1280*720*3];
};

int _tmain(int argc, _TCHAR* argv[])
{
	int begin, end, n=0;

	struct shm_remove
	{
		shm_remove() { shared_memory_object::remove("MySharedMemory"); }
		~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
	} remover;

	managed_shared_memory segment(create_only, "MySharedMemory", sizeof(VideoFrame)*24);
	VideoFrame *frame = segment.construct<VideoFrame>("frame")();
	while(true)
	{
		if(n==0)
		{
			begin = ::GetTickCount();
		}
		frame->width = 1280;
		frame->height = 720;
		scoped_lock<interprocess_mutex> lock(frame->mutex);
		memcpy(frame->data, dummy, sizeof(dummy));
		n++;
		if(n==1000)
		{
			end = ::GetTickCount();
			float t = (end-begin)/1000.0f;
			int rate = (int)(1000/t);
			printf("write rate=%d\r\n", rate);
			n = 0;
		}
		
	}
	segment.destroy<VideoFrame>("frame");

	return 0;
}

 

如果更换成在循环内部分配内存,再释放,则复制频率下降到3200左右。因此在设计大数据量复制的应用程序时,最好不要频繁创建对象和析构对象,这点和进程内的程序开发是一致的。