0
点赞
收藏
分享

微信扫一扫

Unreal C++ API Reference[Accumulate.h]{虚幻4.27底层求和函数性能}

小_北_爸 2022-01-20 阅读 44
Accumulate.h
求和的一些函数,B站视频有更详细的讲解

UE4虚幻C++学习记录

Unreal Engine 4.27 Documention
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreTypes.h"
#include "Templates/Invoke.h"

// TPlus<T> specifically takes const T& and returns T.
// TPlus<> (empty angle brackets) is late-binding, taking whatever is passed and returning the correct result type for (A+B)
template<typename T = void>
struct TPlus
{
	FORCEINLINE T operator()(const T& A, const T& B) { return A + B; }
};
template<>
struct TPlus<void>
{
	template<typename U, typename V>
	FORCEINLINE auto operator()(U&& A, V&& B) -> decltype(A + B) { return A + B; }
};


namespace Algo
{
	/**
	* Sums a range by successively applying Op.
	*
	* @param  Input  Any iterable type
	* @param  Init  Initial value for the summation
	* @param  Op  Summing Operation (the default is TPlus<>)
	*
	* @return the result of summing all the elements of Input
	*/
	template <typename T, typename A, typename OpT>
	FORCEINLINE T Accumulate(const A& Input, T Init, OpT Op)
	{
		T Result = MoveTemp(Init);
		for (const auto& InputElem : Input)
		{
			Result = Invoke(Op, MoveTemp(Result), InputElem);
		}
		return Result;
	}

	/**
	 * Sums a range.
	 *
	 * @param  Input  Any iterable type
	 * @param  Init  Initial value for the summation
	 *
	 * @return the result of summing all the elements of Input
	 */
	template <typename T, typename A>
	FORCEINLINE T Accumulate(const A& Input, T Init)
	{
		return Accumulate(Input, MoveTemp(Init), TPlus<>());
	}

	/**
	* Sums a range by applying MapOp to each element, and then summing the results.
	*
	* @param  Input  Any iterable type
	* @param  Init  Initial value for the summation
	* @param  MapOp  Mapping Operation
	* @param  Op  Summing Operation (the default is TPlus<>)
	*
	* @return the result of mapping and then summing all the elements of Input
	*/
	template <typename T, typename A, typename MapT, typename OpT>
	FORCEINLINE T TransformAccumulate(const A& Input, MapT MapOp, T Init, OpT Op)
	{
		T Result = MoveTemp(Init);
		for (const auto& InputElem : Input)
		{
			Result = Invoke(Op, MoveTemp(Result), Invoke(MapOp, InputElem));
		}
		return Result;
	}

	/**
	* Sums a range by applying MapOp to each element, and then summing the results.
	*
	* @param  Input  Any iterable type
	* @param  Init  Initial value for the summation
	* @param  MapOp  Mapping Operation
	*
	* @return the result of mapping and then summing all the elements of Input
	*/
	template <typename T, typename A, typename MapT>
	FORCEINLINE T TransformAccumulate(const A& Input, MapT MapOp, T Init)
	{
		return TransformAccumulate(Input, MapOp, MoveTemp(Init), TPlus<>());
	}
}

上面是官方代码,下面是同代码的C++,虚幻改了一些东西。通过计算求和同一个vector里面的1000万个数组的和(和会溢出),结果是……
#include <iostream>
#include <vector>
#include <functional>
#include <vector>
#include<time.h>
using namespace std;

template<typename T = void>
struct TPlus
{
	__forceinline T operator()(const T& A, const T& B) { return A + B; }
};
template<>
struct TPlus<void>
{
    template<typename U, typename V>
	__forceinline auto operator()(U&& A, V&& B) -> decltype(A + B) { return A + B; }
};

template <typename T, typename A, typename OpT>
__forceinline T Accumulate(const A& Input, T Init, OpT Op)
{
	T Result = std::move(Init);
	for (const auto& InputElem : Input)
	{
		Result = std::invoke(Op, std::move(Result), InputElem);
	}
	return Result;
}

int main() {
	int begintime, endtime;
	vector<int> vet;
	//0 1 2 3 4 =10
	for (int i = 0; i < 10000000; i++) {
		vet.push_back(i);
	}

	begintime = clock();
	cout << "虚幻的求和函数" << endl;
	cout << Accumulate<int, vector<int>, TPlus<int>>(vet, 0, TPlus<int>()) << endl;
	endtime = clock();
	cout<<"Running Time:"<<(endtime-begintime)<<"ms"<< endl;

	begintime = clock();
	int sum = 0;
	for (int i = 0; i < vet.size(); i++) {
		sum += vet[i];
	}
	cout << "我的求和函数" << endl;
	cout << sum << endl;
	endtime = clock();
	cout << "Running Time:" << (endtime - begintime) << "ms" << endl;

    return 0;
}
结果如下,泛型的使用带来了复用性的同时有一定时间的消耗,但是虚幻官方的程序员已经压榨了C++的最后一滴性能,保证泛型的使用。

在这里插入图片描述

举报

相关推荐

0 条评论