当使用具有try/catch/finally的语言时,D的失败/成功/退出范围语句是否仍然有用?D似乎没有最终可以解释为什么在D中使用这些语句.但是使用像C#这样的语言是否有用?我正在设计一种语言,所以如果我看到很多专业人士,我会加入它.
我有一个算法将拜耳图像通道转换为RGB.在我的实现中,我有一个嵌套for循环,它遍历拜耳通道,从拜耳索引计算rgb索引,然后从拜耳通道设置该像素的值.这里要注意的主要事实是每个像素可以独立于其他像素计算(不依赖于先前的计算),因此该算法是并行化的自然候选者.但是,计算依赖于某些预设数组,所有线程将在同一时间访问但不会更改.
然而,当我尝试将主要for与MS 并行化时,我的cuncurrency::parallel_for性能没有提升.事实上,对于在4核CPU上运行的大小为3264X2540的输入,非并行化版本在~34ms内运行,并行化版本运行在~69ms(平均超过10次运行).我确认该操作确实是并行化的(为该任务创建了3个新线程).
使用英特尔的编译器提供tbb::parallel_for了接近完全的结果.为了比较,我开始使用这个算法实现,C#其中我也使用了parallel_for循环,在那里我遇到了接近X4的性能提升(我选择了C++因为这个特定任务C++即使使用单个核心也更快).
有什么想法阻止我的代码很好地并行化?
我的代码:
template<typename T>
void static ConvertBayerToRgbImageAsIs(T* BayerChannel, T* RgbChannel, int Width, int Height, ColorSpace ColorSpace)
{
//Translates index offset in Bayer image to channel offset in RGB image
int offsets[4];
//calculate offsets according to color space
switch (ColorSpace)
{
case ColorSpace::BGGR:
offsets[0] = 2;
offsets[1] = 1;
offsets[2] = 1;
offsets[3] = 0;
break;
...other color spaces
}
memset(RgbChannel, …Run Code Online (Sandbox Code Playgroud) 使用有什么好处boost::any_range?这是一个例子:
typedef boost::any_range<
int
, boost::forward_traversal_tag
, int
, std::ptrdiff_t
> integer_range;
void display_integers(const integer_range& rng)
{
boost::copy(rng,
std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
}
int main(){
std::vector<int> input{ ... };
std::list<int> input2{ ... };
display_integers(input);
display_integers(input2);
}
Run Code Online (Sandbox Code Playgroud)
但是使用模板参数可以实现具有更高效率的相同功能,该参数满足ForwardRange概念:
template <class ForwardRange>
void display_integers(const ForwardRange& rng)
{
boost::copy(rng,
std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
所以我在寻找使用any_range值得的场景.也许我错过了什么.
我目前正处于一个性能至关重要的项目中.以下是我对此问题的一些问题.
问题1
我的项目涉及很多.boost::shared_ptr我知道在运行时创建共享指针boost::make_shared很慢,因为它需要跟踪引用有很多开销.我想知道如果已经创建了boost共享指针,那么这两个语句是否有相同的性能或者一个比另一个快.如果常规指针更快并且我已经有共享指针我有什么选项来调用共享指针指向的方法?
statement1: sharedptr->someMethod(); //here the pointer is a shared ptr created by boost::make_shared
statement2: regularptr->someMethod(); //here the pointer is a regular one made with new
Run Code Online (Sandbox Code Playgroud)
问题2
我有一个快速调用的实例方法,std::vector<std::string>每次都在堆栈上创建一个.我决定将该向量指针存储在静态std :: map(即)中std::map<std::String,std::vector<std::string>*>.如果键的映射中不存在向量(可以是方法的名称).创建了有效的矢量地址并将其添加到地图中.所以我的问题是"是否值得在地图上搜索矢量地址并返回一个有效的地址,而不仅仅是在堆栈上创建一个地址std::vector<std::string> somevector.我也想了解一下std::map找到的表现.
关于这些问题的任何想法将不胜感激.
我写了这个分区函数:
template <class I, class P> I partition(I beg, I end, P p)
{
I first = beg;
while(beg != end) {
if(!p(*beg))
beg++;
else {
// if(beg != first) - EDIT: add conditional to prevent swapping identical elements
std::swap(*beg, *first);
first++;
beg++;
}
}
return first;
}
Run Code Online (Sandbox Code Playgroud)
我用几个输出测试了它,我没有发现它有什么问题.
标准库分区功能相当于:
template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator partition (BidirectionalIterator first,
BidirectionalIterator last, UnaryPredicate pred)
{
while (first!=last) {
while (pred(*first)) {
++first;
if (first==last) return first;
}
do {
--last;
if …Run Code Online (Sandbox Code Playgroud) ISO C++ 11 24.3:
template <class InputIterator, class Distance>
void advance(InputIterator& i, Distance n);
// ...
template <class ForwardIterator>
ForwardIterator next
(
ForwardIterator x,
typename std::iterator_traits<ForwardIterator>::difference_type n = 1
);
Run Code Online (Sandbox Code Playgroud)
为什么std::next不接受InputIterator?
我正在考虑的一个合法用例是:
first = find(next(first, x), last, 11); // ...
Run Code Online (Sandbox Code Playgroud)
我找到了合适的DR:
next/prev返回递增的迭代器而不更改原始迭代器的值.但是,即使这样也可能使a无效InputIterator.AForwardIterator需要保证"多通"属性.
但我不明白多通/失效是如何与此相关的.使用相同的多通道/无效的推理,我们甚至可以禁止std::find为InputIteratorS:
template<class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& value);
Run Code Online (Sandbox Code Playgroud)
没有什么特别之处std::next的比较std::find或std::vector::insert(pos, first, last) …
我的代码需要从以下函数连续计算一个值:
inline double f (double x) {
return ( tanh( 3*(5-x) ) *0.5 + 0.5);
}
Run Code Online (Sandbox Code Playgroud)
分析表明程序的这一部分是花费大部分时间的地方。由于该程序将运行数周甚至数月,我想优化此操作并正在考虑使用查找表。
我知道查找表的效率取决于表本身的大小以及它的设计方式。目前我不能使用少于 100 MB,最多可以使用 2GB。矩阵中两点之间的值将被线性插值。
使用查找表会比进行计算更快吗?此外,使用 N 维矩阵是否会比一维 std::vector 更好,并且不应跨越的表大小的阈值(如果有)是多少?
c++ ×7
stl ×3
boost ×2
performance ×2
algorithm ×1
boost-range ×1
c# ×1
c++11 ×1
caching ×1
d ×1
optimization ×1
parallel-for ×1
ppl ×1
shared-ptr ×1
tbb ×1
type-erasure ×1
winapi ×1