按需加载缩略图或将完整大小加载到内存中哪个更好? [英] Which is better, Load Thumbnail on demand or Load full size in memory?

查看:111
本文介绍了按需加载缩略图或将完整大小加载到内存中哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要一些有关速度和效率的建议.我有一个程序可以将一堆图像加载到一个数组中,并在屏幕上以网格形式显示它们.我将全尺寸图片(可以是数百张)加载到数组中,而不是加载每个image.fromfile并一次显示16张. fromfile方法太慢,无法按需加载16张图像以保持每页显示(因为它们是400x300).另一种方法是在保存时将这些图像调整为我需要的大小(140x100),然后加载并存储,然后将较小的缩略图加载到数组中.较小的图像将占用较少的内存来存储,但是我不知道它会更快还是更有效率.另外,我确实需要完整尺寸的图像,因此最终将不得不加载它.我问的原因是因为要加载400张图像几乎要花费大量内存.太多了.

我在正确地解释自己吗?

感谢您的任何建议... :)

-Josh

Hey all,

I need some advice for speed and efficiency. I have a program that loads a bunch of images into an array and displays them in grid form on the screen. I load the full size images (can be hundreds) into the array instead of loading each image.fromfile and display them 16 at a time. The fromfile approach is too slow to load the 16 images on demand to keep up per page (as they are 400x300). The alternative is to load in and store these images resized to the size I need (140x100) once as I am saving them, and then load the smaller thumbnails into the array. The smaller images would take less memory to have stored but I don''t know if it would be any faster or more efficient. Also, I do need the full size image so I would have to load it eventually. The reason I ask is because it is taking almost a gig of memory to load the 400 images. That''s a lot.

Am I explaining myself correctly?

Thanks for any advice... :)

-Josh

推荐答案

我似乎有点人为的问题.您永远不会一次查看400张图像.但是,由于加载时间而导致的延迟优化问题很有趣.

您只需要加载一个屏幕上可以看到的尽可能多的图像.经过重新采样,仅一张像素数量比屏幕高几倍的高分辨率图像就不应该多了.我在讲这个示例只是为了进行速度比较-它花费了相当多的时间,因此渲染图像覆盖的整个屏幕将花费可接受的时间.

如果所有图像的访问都是连续的,并且最典型的显示顺序是滚动一行,则可以通过将某些100%到200%的图像(与屏幕大小相比)放入内存中的某些图像缓存中来改善这种延迟.考虑一些典型的示例:一个典型的屏幕显示8x4图像,4行8图像.整个节目可能很多GB,但一次只能看到32张图像.您可以在内存中保留一些32 * 3图像:屏幕上方4行,屏幕上显示4行,下方4行.如果将所有节目仅滚动一行或四行,则首先仅渲染已加载到内存中的图像.当已经显示了滚动视图图像时,您可以从内存中删除1-4行并加载1-4行以将4 + 4 + 4行保留在内存中.这样,当用户不必等待图像加载时,图像加载就会在后台发生.

通过这种方式,如果用户倾向于在任何方向上将演示文稿大部分滚动到1-4行(其中4行是全屏),则几乎会发生100%的缓存匹配. 缓存未命中的情况很少发生;并且由于以下原因,如果首先加载要查看的图像,则延迟仍然可以接受
这种策略对于所有缓存(例如CPU缓存)的使用都是典型的.原理几乎相同,并在此处进行了很好的解释:
I looks like a bit of is artificial problem. You never look at 400 images at a time. However the problem of optimization of latency due to loading time is interesting.

You need to load only as many images as you can see at one screen. It should not much more that just one high-resolution image with number of pixels several times more than the screen has, with re-sampling. I''m talking about this example just for speed comparison — it takes quite acceptable amount of time, so rendering the whole screen covered by images would take acceptable amount of time.

This latency can be improved be putting in some image cache some 100-200% of images (compared to screen-size) in memory in case the access of all images is consecutive, and most typical order of presentation is scrolling by one row. Consider some typical example: a typical screen shows 8x4 images, 4 rows of 8 images. The whole show may be many gigabytes, but you look only at some 32 images at a time. You can keep in memory some 32 * 3 image: 4 rows above the screen, 4 rows shown on screen and 4 rows below. If you scroll all your show just by one row or by 4 rows, you first render only the images already loaded in memory. When scrolled in view images are already presented, you can remove 1-4 rows from memory and load 1-4 rows to keep 4 + 4 + 4 rows in memory. In this way, the image loading happens behind the scene, when the user does not have to wait for it.

In this way, if the user tend to scroll the presentation mostly be 1-4 rows (where 4 rows is the full screen) in any direction, almost 100% of cache hits is experienced. Cache miss happens more rarely; and the latency is still acceptable if the images coming in view are loaded first, by the reasons

This strategy is typical for all uses of cache, such as CPU cache. The principles are pretty much the same and are well explained here: http://en.wikipedia.org/wiki/Cache_memory[^].

—SA


嗨乔什,
我们是否应该考虑一种非常不同的方法,让我们保留两组小"和大"类型的图像.

正如您建议的那样,通过在延迟绑定模型中首次加载缩略图来将这些缩略图另存为小"图像,尝试重新使用这些图像.
Hi Josh,
Shall we think about a very different approach, let us keep two set of images of ''small'' and ''large'' type.

As you suggested try by loading thumbnail for the very first time in a lazy binding model as save back these thumbnail as ''small'' images, try to re-sue these image.


这篇关于按需加载缩略图或将完整大小加载到内存中哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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