内存上的Jpeg vs磁盘上的jpeg [英] Jpeg on MEMORY vs jpeg on disk

查看:64
本文介绍了内存上的Jpeg vs磁盘上的jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个缓冲区,我想将其转换为jpg60并将其保存在内存中,使用Java语言在不到100-200ms的时间内通过http响应发送。

我的测试后发现将它保存为磁盘上的jpg文件比内存更快。

我的问题是:我想将内存中的缓冲区保存为jpg60并将其发送到客户端不到100-200ms ???

i have a buffer that i want to convert it to jpg60 and save it on memory to be sent on http response using Java language in less then 100-200ms.
after my tests i found that save it as jpg file on disk is faster then on memory.
my question is : i want to save the buffer on memory as jpg60 and send it to client side in less then 100-200ms???

BufferedImage out = new BufferedImage(col, row, BufferedImage.TYPE_INT_RGB);

		int ii = 0, jj = 0;

		for (int i = 0; i < row; i--) {
			for (int j = 0; j < col; j++) {
				out.setRGB(i, j, m_outData[(i * col) + j]);
			}
		}

// ByteArrayOutputStream outStream = new ByteArrayOutputStream();

		try {

			// ImageOutputStream ios =
			// ImageIO.createImageOutputStream(outStream);
			// Iterator<ImageWriter> iter =
			// ImageIO.getImageWritersByFormatName("jpeg");
			// ImageWriter writer = iter.next();
			// ImageWriteParam iwp = writer.getDefaultWriteParam();
			//
			// iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
			// iwp.setCompressionQuality(0.60f);

			// writer.setOutput(new MemoryCacheImageOutputStream(outStream));
			// writer.write(null, new IIOImage(out, null, null), jpegParams);

			// // write to file on hard disk
			outPut = new File(prefix + "test.jpg");

			// writer.setOutput(outPut);
			// writer.write(null, new IIOImage(out, null, null), jpegParams);

			ImageIO.write(out, "jpg", outPut);

			// write as byte array to transfered to client
			// ImageIO.write(out, "jpg", outStream);
			// outStream.flush();

			// imageToByte = outStream.toByteArray();
			// ===========================================

			outPut = null;

		} catch (Exception e) {
			System.out.println("saveImage " + e.getMessage());
		}





我的尝试:



FileOutPutStream,ByteArrayOutPutStream



What I have tried:

FileOutPutStream, ByteArrayOutPutStream

推荐答案

你的测试很可能是错误的。保存到内存流总是比保存到磁盘更快。



有什么问题?我不知道,因为你发布的代码没有显示你是如何测试它以及如何定时的。
Your test is more than likely wrong somehow. Saving to a memory stream is always going to be faster than saving to disk.

What is wrong? I have no idea since the code you posted doesn't show how you tested this and how it was timed.


引用:

我的测试后发现将它保存为磁盘上的jpg文件比内存更快。

after my tests i found that save it as jpg file on disk is faster then on memory.

基本上,这是不可能的。

你没有测量相同的事情。

你的jpg文件首先内置在内存中,然后写入磁盘。最后一次额外的操作是不可能使整个事情变得更快。



建议:使用分析器查看你在哪里花时间。

确保您的代码对内存和磁盘都做同样的事情。

Basically, It is impossible.
You don't measure the same thing.
your jpg file is first built in memory, and then wrote to disk. It is impossible the last operation which is extra makes the whole thing faster.

Advice: use a profiler to see where you spend time.
Make sure your code do the same thing for both memory and disk.


这篇关于内存上的Jpeg vs磁盘上的jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆