如何优化 xml 输出以缩短数据传输时间?
你好,
我有一个调用基本 API URL 的 cron。输出是 xml,因此我使用 php 的 file_get_contents()
来获取要处理的数据。
我的问题是,xml 的输出格式是否会影响从一台服务器到另一台服务器的传输时间? cron 每十分钟运行一次,我不希望 cron 在某个时刻重叠,因为数据处理对时间敏感。
ex :
<?xml version="1.0"?><api><data>sometext here</data></api><!-- As one line -->
<?xml version="1.0"?>
<api>
<data>sometext here</data>
</api> <!-- As multiple lines -->
请注意,这只是一个 xml 示例,我的数据使 xml 输出超过 2000 行长。
我已经测试过它,对于任何类型的 xml 来说似乎都是相同的(可能相差微秒)。有什么办法可以加快速度吗?
HI,
I have a cron that calls a basic API URL. The output is xml so I use php's file_get_contents()
to get the data to process.
My question is, Does the output format of the xml make a difference in the transfer time from one server to another? The cron is running every ten minutes and I don't want the crons to overlap at some point because the data processing is time sensitive.
ex :
<?xml version="1.0"?><api><data>sometext here</data></api><!-- As one line -->
<?xml version="1.0"?>
<api>
<data>sometext here</data>
</api> <!-- As multiple lines -->
Note that this is only an xml example, the data of mine makes the xml output 2000+ lines long.
I have tested it and it seems like it is the same(maybe microseconds apart) for any type of xml. Is there any way of speeding that up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不削减 XML 输出长度本身,我看不到大幅加速的可能性。
删除不必要的空白以进行传输非常有用,因为它可以降低传输速度。即使没有那么多。你的 xml 越长,其中不必要的空格越多(解析 xml 不需要。仅用于人类可读性),你赢得的胜利就越大。如果你有很深的层次结构和许多空白以使其可读,那么你会收获很多。如果不是,那么剥离不会大幅降低传输速度。
我看到的唯一方法是生成更小的 xml 数据。
If you don't cut your XML output Length itself i don't see a possebility for a big speedup.
Stripping unnecessary whitespaces for transport is pretty usefull because it could reduce transport speed. even if it is not that mutch. the longer your xml is and the more unnecessary whitespaces you have there (unnecessary for parsing the xml. only used for human readability) the more you will win. If you have a deep hirarchy and many white spaces to make it readable then you will gain mutch. If not then the stripping won't reduce transfer speed that mutch.
The only way i would see is to generate smaller xml data.
也许你可以尝试像 gzip 这样的压缩。
Maybe you could try a compression like gzip.
使用 gzip 压缩您的页面。您可以通过 mod_gzip(apache 模块)执行此操作,或者如果您使用 PHP,请在 php.ini 中设置 zlib.output_compression = On。这会将您的纯文本内容压缩到原始大小的 20% 左右。
此外,如果您关心绝对最小的可能数据,您可以使用 JSON 来发送数据,而不是使用 XML。
Compressed your pages using gzip. You can do this via mod_gzip (apache module) or if you are using PHP, set zlib.output_compression = On in php.ini. This will compress your plaintext content to ~20% of it's original size.
Also, if you are concerned about the absolute smallest possible data, instead of XML, you can send it out using JSON.