2

I want all albums from facebook and get the src property of the cover photo. This is where it became tricky. I wrote the following code to do this, but i'm wondering if this can't be done in a more efficient way?

var facebookClient = new FacebookClient(accessToken);

        var sw = Stopwatch.StartNew();
        dynamic facebookAlbums = facebookClient.Get("/fql",
            new
            {
                q = new
                {
                    coverPids = "select name, link,aid, cover_pid from album where owner = 56615007038 AND photo_count > 0 ORDER BY created desc",
                    coverSrcs = "select src, src_big,aid from photo where pid in (select cover_pid from #coverPids)"
                }
            });
        sw.Stop();
        Console.WriteLine("fql query took: " + sw.ElapsedMilliseconds);
        sw.Restart();
        var coverPids = ((Facebook.JsonArray)facebookAlbums.data).Cast<dynamic>().FirstOrDefault(i => i.name == "coverPids");
        var coverSrcs = ((Facebook.JsonArray)facebookAlbums.data).Cast<dynamic>().FirstOrDefault(i => i.name == "coverSrcs");

        if (coverPids != null && coverSrcs != null && coverPids.fql_result_set != null && coverSrcs.fql_result_set != null)
        {
            var coverSrcsFqlResultSet = coverSrcs.fql_result_set as IEnumerable<dynamic>;
            var coverPidsFqlResultSet = coverPids.fql_result_set as IEnumerable<dynamic>;
            var test = coverPidsFqlResultSet.Join(coverSrcsFqlResultSet, album => album.aid, photo => photo.aid, (album, photo) => new
            {
                name = album.name,
                link = album.link,
                aid = album.aid,
                cover_pid = album.cover_pid,
                src = photo.src,
                src_big = photo.src_big
            });
        }
        sw.Stop();
        Console.WriteLine("processing took: " + sw.ElapsedMilliseconds);
        Console.ReadLine();

The times are now as follows when performing this 10 times in a loop:

  • fql query took: 717
  • processing took: 204
  • fql query took: 304
  • processing took: 0
  • fql query took: 265
  • processing took: 0
  • fql query took: 251
  • processing took: 0
  • fql query took: 220
  • processing took: 0
  • fql query took: 237
  • processing took: 0
  • fql query took: 225
  • processing took: 0
  • fql query took: 224
  • processing took: 0
  • fql query took: 225
  • processing took: 0
  • fql query took: 219
  • processing took: 0
on5sl
  • 51
  • 8
  • maybe if you post the times of the "fql query" and the "processing" someone can help you improve the code – Mauricio Gracia Gutierrez Dec 26 '13 at 13:52
  • done! 608 and 249 milliseconds. – on5sl Dec 26 '13 at 13:54
  • maybe you can have a thread that starts to take the first album image and title and start to show it, so that albums start to appear as they load – Mauricio Gracia Gutierrez Dec 26 '13 at 13:56
  • I'm having hard time to believe the processing took 250ms.. – Letterman Dec 26 '13 at 14:20
  • @on5sl can you please put this within a for loop, and run it 20 times or so..? so you'll get some sort of average for the timing – Letterman Dec 26 '13 at 14:21
  • When i do this 10 times: fql query took: 717 processing took: 204 fql query took: 304 processing took: 0 fql query took: 265 processing took: 0 fql query took: 251 processing took: 0 fql query took: 220 processing took: 0 fql query took: 237 processing took: 0 fql query took: 225 processing took: 0 fql query took: 224 processing took: 0 fql query took: 225 processing took: 0 fql query took: 219 processing took: 0 – on5sl Dec 26 '13 at 14:45

0 Answers0