V
V
Valera Udav2014-06-29 17:33:48
PHP
Valera Udav, 2014-06-29 17:33:48

Slow XML parsing using PHP. How to increase speed?

There is a task to parse xml files from vimeo video hosting. Implementation script:

<?php
function vimeo_parse($video_id) {
  $url = "http://vimeo.com/api/v2/video/".$video_id.".xml";
  $xml = simplexml_load_file($url);

  $vimeo_title = $xml->video->title;
  $vimeo_thumb = $xml->video->thumbnail_large;

  return array($vimeo_title,$vimeo_thumb,$video_id);
}

// пример использования
// list ($vimeo_title,$vimeo_thumb,$vimeo_id) = vimeo_parse("27973852");
// echo $vimeo_title;
// echo $vimeo_thumb;
// echo $vimeo_id;
?>

On the page you need to display about 20 videos. Each parsing takes approximately 0.4 seconds, which is 20 * 0.4 = 8 seconds of parsing (+loading the page itself), which is not good at all. How can you avoid such a long script work?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
pi314, 2014-06-29
@TARAKANhoy

LIBXML_COMPACT?
And, in general, the problem, it seems, is an architectural plan. If the files are small, then the time is mostly spent downloading via HTTP. Download 20 files, put on disk and compare the performance. If confirmed, only some clever pre-caching will help.

A
Alexander Borisovich, 2014-06-29
@Alexufo

Are you polling the server, and why should it be faster? Cache poll results, load asynchronously via js on the client.

A
Alexander N++, 2014-06-29
@sanchezzzhak

Create a background script that will download data to you.
For large XML 10mb-2gb use XmlReader to bypass the nodes to read the SimpelXMl node.
Polling the server constantly is not an option, sooner or later it will not be able to give you XML, you will show the visitor not what he wanted to see and he will leave the site

A
Alexey, 2014-06-29
@rdifb0

You need to get data that won't change much. Why not parse everything in advance, where does the video_id come from?
All the time is spent on HTTP for sure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question