小编Nat*_*ate的帖子

可以根据模板参数有条件地添加 C++ 元组元素类型吗?

我正在尝试根据一些编译时条件有条件地将类型添加到元组模板类型,如下所示:

template <typename T>
class Base { ... }

template <int I>
class Derived : public Base<std::tuple<std::conditional<I % 8 == 0, int,    void>::type,
                                       std::conditional<I % 4 == 0, double, void>::type,
                                       std::conditional<I % 2 == 0, float,  void>::type>>
{ ... }

Run Code Online (Sandbox Code Playgroud)

我知道这不是有效代码,但从概念上讲,我试图有条件地将类型添加到元组列表中。我希望在条件解析为void.

有没有办法做这样的事情?

c++ tuples template-meta-programming

12
推荐指数
1
解决办法
609
查看次数

为什么多线程放慢了速度

我正在尝试将RGB值的字节数组保存为png图像,如下所示:

byte[] imgArray = ...;
int canvasSize = 512;

ColorModel c = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), null, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

Image image = Toolkit.getDefaultToolkit().createImage(
            new MemoryImageSource(canvasSize, canvasSize, c, imgArray, 0, canvasSize));

BufferedImage bimage = new BufferedImage(canvasSize, canvasSize, BufferedImage.TYPE_BYTE_GRAY);

// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(image, 0, 0, null); //This is what takes all the time
bGr.dispose();

ImageIO.write(bimage, "PNG", new File(uniqueFileName));
Run Code Online (Sandbox Code Playgroud)

我正在使用FixedThreadpool同时保存多个图像.我使用的线程越多(最多我的计算机上的可用核心数),保存过程所需的时间就越长.在6个线程上运行所需的时间几乎是在一个线程上运行的两倍.

为什么多线程需要这么长时间?内存交换?我可以避免这个问题吗?

另外,如果我有更好的方法从阵列中保存png,请告诉我.

编辑显示图片被保存为不同的图像,而不是相互覆盖.

java multithreading javax.imageio

3
推荐指数
1
解决办法
275
查看次数