0

I ran into an interesting Issue today working on a Wall Feed Plugin. A majority of videos posted to the feed via youtube have autoplay enabled.

"source": "http://www.youtube.com/v/IXTS79iDTNA?version=3&autohide=1&autoplay=1",

I am trying to rewrite that url before embeding using php. How would you do this?

So far i have tried using strtr(); with array, seems though if there are alot of videos in the feed, things seem to slow down alot.


    /* $fvalue[source] is the video url in graph api */ 
    if($fvalue[source]){
            $reWrite = array("autoplay=1" => "autoplay=0");
        $getEmbed = $fvalue[source];
        $strAuto = strtr($getEmbed, $reWrite);
        echo '<object><embed src="'.$strAuto.'"></embed></object>';
    }
ShawnDaGeek
  • 4,145
  • 1
  • 22
  • 39

1 Answers1

2

It is slow because of the strstr. Roughly speaking, str_replace is 30-50 times faster.

//This code should be at least 30 times faster.
if($fvalue[source]){
    $strAuto = str_replace("autoplay=1", "autoplay=0", $fvalue[source]);
    echo '<object><embed src="'.$strAuto.'"></embed></object>';
}
Laith Shadeed
  • 4,271
  • 2
  • 23
  • 27
  • This took about 900ms off the render time thank you. Reward goes to you but i can not reward you for 1 more hour. – ShawnDaGeek Nov 12 '11 at 01:37
  • 1
    mm, 900ms is also big number in terms of user-experience. Then maybe is it better to do this process off-line and cache it. – Laith Shadeed Nov 12 '11 at 09:33
  • Doing offline is not an option unfortunately, since the plugin pulls this information live from graph each time. Benchmarks how ever did showed no improvement in performance when i added the full array, including all flash objects from all sources known to use the auto-play features, including music. I am looking into using preg_replace() since i am not using regular expression. If you have any knowledge of this use i would appreciate any insight. – ShawnDaGeek Nov 12 '11 at 17:09
  • here is a sample of the plugin https://shawnsspace.com/plugins/TimeLineFeed.php?pageid=StuffJustForFun&since=900+days+ago&until=now&limit=16&type=posts&fh=750 loading a page with alot of videos. All have auto play enabled. Avg is 2000ms with both strtr, and str_replace. – ShawnDaGeek Nov 12 '11 at 17:16
  • 1
    preg_replace is slower than str_replace. Check this fo more info http://www.simplemachines.org/community/index.php?topic=175031.0 If you can send me your full or sample file, I will benchmark it using xhprof, and give you better alternatives solutions. – Laith Shadeed Nov 12 '11 at 17:17
  • 1
    if the feeds are pulled in realtime. what about generating those feeds each 1 min using cron, and let this plugin reads them from file system ? then even to make things faster, cache the result of opening the feeds file in apc so that it will not be opened with each request. – Laith Shadeed Nov 12 '11 at 17:21
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4933/discussion-between-laith-shadeed-and-shawn-e-carter) – Laith Shadeed Nov 12 '11 at 17:27