简体   繁体   English

浏览器中的图像表现为背景重复CSS

[英]Images in browsers performance for background-repeat CSS

Does any one know which is going to be better for a browser loading time between these two options: 有没有人知道哪个浏览器在这两个选项之间加载时间会更好:

background-image:url('1by1px.png');

or 要么

background-image:url('10by10px.png');

A 1px by 1px semi transparent png repeated for the div. 为div重复1px乘1px半透明png。 Or a larger one say 10px by 10px. 或者更大的一个说10px乘10px。

There must be some kind of looping that has to be done to display the repeated image in the browser, and so I wondered if the image which is 1px by 1px causes alot of looping to get the image displayed that it may in fact be less speedy than a larger dimensioned image with less looping? 必须要有某种循环才能在浏览器中显示重复的图像,所以我想知道1px by 1px的图像是否会导致很多循环以显示图像实际上可能不那么快比较大尺寸的图像具有较少的循环?

Of course the counter argument is image size is smaller for 1by1 compared to 10by10, but doesn't mean its better to be smaller because looping many times might not scale as good as looping a large image size slightly less often. 当然,计数器参数是1by1的图像尺寸小于10by10,但并不意味着它更小更好,因为多次循环可能不会像稍微小一些地循环大图像尺寸那样好。

Does any know more about which would be better and how browsers handle situations like this? 有没有更多关于哪个会更好以及浏览器如何处理这样的情况?

When not repeating the background image, the time required to render depends on only the final scaled image, not the original one. 不重复背景图像时,渲染所需的时间仅取决于最终缩放的图像,而不是原始图像。

The image in a file is compressed as PNG format, but after being loaded by browser, it is in RGBA bitmap format (4 bytes for a pixel). 文件中的图像被压缩为PNG格式,但在浏览器加载后,它采用RGBA位图格式(像素为4个字节)。 When repeating a background, (let say on Intel x86), the native code of browser would use REP MOVSD to move the bitmap data from RAM to video memory (this is standard sequence, might be different on various implementations of graphics driver or specific GPU). 当重复背景时(假设在Intel x86上),浏览器的本机代码将使用REP MOVSD将位图数据从RAM移动到视频内存(这是标准序列,可能在图形驱动程序或特定GPU的各种实现上有所不同)。

Assume that the dimensions of the HTML DIV which contains the background would be 100x100. 假设包含背景的HTML DIV的尺寸为100x100。

For the only-1 pixel image: the browser programme has to exec 10 thousand 'REP MOVSD' instructions. 对于唯一的1像素图像:浏览器程序必须执行1万条“REP MOVSD”指令。

For the 10x10 image: with each repeated image, the browser programme has to exec 'REP MOVSD' only 10 times (1 time calling to 'REP MOVSD' can render 1 pixel line (pixel row) of the image). 对于10x10图像:对于每个重复的图像,浏览器程序必须仅执行“REP MOVSD”10次(1次调用“REP MOVSD”可以渲染图像的1像素行(像素行))。 So in this case, the number of 'REP MOVSD' instructions executed would be only 10x100 times (10 times in 1 image, 100 repeated images). 因此,在这种情况下,执行的“REP MOVSD”指令的数量仅为10x100次(1张图像中的10次,100次重复图像)。 This takes totally 1 thousand 'REP MOVSD' . 这需要1千'REP MOVSD'

Therefore, the final background based on the bigger image would be rendered faster . 因此,基于较大图像的最终背景将更快地呈现

More notes: The above explanation doesn't mean the performance is exactly 10 times better for the 10x10 image. 更多说明: 上述说明并不意味着10x10图像的性能正好好10倍。 A 'REP MOVSD' (with CX=9999 for example) is only 1 single CPU instruction but still requires 9999x4 bytes to be transfered through data bus. 'REP MOVSD'(例如CX = 9999)只有1个单CPU指令,但仍需要9999x4字节通过数据总线传输。 If using 9999 simple 'MOV's, that much of data still have to go thru' data bus, however, CPU has to execute 9998 instructions more. 如果使用9999个简单的'MOV',那么大部分数据仍然必须通过'数据总线,但是,CPU必须执行更多的9998指令。 A more clever browser would create a pre-rendered bitmap for the background with replicated images; 一个更聪明的浏览器将为具有复制图像的背景创建预渲染位图; so each time it needs to transfer to video memory, it needs just only 100 'REP MOVSD' (100 is the number of pixel rows in the final background, assumed above) instead of 10 thousand or 1 thousand. 因此,每次需要传输到视频内存时,它只需要100'REP MOVSD'(100是最终背景中的像素行数,假设如上)而不是1万或1千。

I agree with Paul answer. 我同意保罗的回答。 I did few rough test with Google Chrome developer tool recently. 我最近使用谷歌Chrome开发工具进行了一些粗略测试。 I used different size of semi-transparent png images on top of a background image and use page paint time to see how long do it take to refresh the screen. 我在背景图像上使用了不同大小的半透明png图像,并使用页面绘制时间来查看刷新屏幕需要多长时间。

Here is the result: 结果如下:

Time to refresh without -webkit-transform hack (rounded): 没有-webkit-transform hack(舍入)的时候刷新:

2x2 image : 65-160ms 2x2图像 :65-160ms

10x10 image : 60-150ms 10x10图像 :60-150ms

100x100 image : 55-135ms 100x100图像 :55-135ms

1000x1000 image : 55-130ms 1000x1000图像 :55-130ms

Time to refresh with -webkit-transform hack (rounded): 使用-webkit-transform hack(舍入)刷新时间:

2x2 image : 40-120ms 2x2图像 :40-120ms

10x10 image : 30-90ms 10x10图像 :30-90ms

100x100 image : 30-90ms 100x100图像 :30-90ms

1000x1000 image : 30-90ms 1000x1000图像 :30-90ms

Just like what Paul said, bigger image is take shorter time to load(refresh), than smaller image. 就像Paul所说的那样,较大的图像比较小的图像需要更短的加载(刷新)时间。 But, it seem it is getting less effective after the image getting bigger than 10px. 但是,在图像大于10px之后,它似乎变得不那么有效了。 I don't see much difference between 100x100 and 1000x1000. 我没有看到100x100和1000x1000之间有太大差异。

In my opinion, an huge image won't give you a noticeable result, and it might increase the loading time. 在我看来,巨大的图像不会给你一个明显的结果,它可能会增加加载时间。 So, I think any size around 10 - 100 is good enough for performance and loading time. 所以,我认为任何大小在10到100之间的尺寸都足以满足性能和加载时间。

But still, different image might have different result, I think you should test your site with page paint time tool in Google Chrome developer tool for accurate result. 但是,不同的图像可能会有不同的结果,我认为您应该使用Google Chrome开发工具中的页面绘制时间工具测试您的网站,以获得准确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM