1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

iterating IEnumerable is very slow

Discussion in 'C#' started by =OTS=G-Man, Aug 28, 2010.

  1. =OTS=G-Man

    =OTS=G-Man New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello everyone,

    I am working on a C# app that uses the YouTube .NET API but have come up with an issue, and I'm not sure if its the .NET Wrapper or just something I'm Doing.

    Ok heres the problem, I request the list of videos the logged in user has and I get it back as a IEnumerable, now from everywhere I read, IEnumerable is supposed to be better then List and what not. One site showed an example of 1,000,000 Entries in IEnumerable could be iterated in just over 1 second. My problem is that i have 458 Entries and its taking 45 seconds or more to populate my listview or use ElementAt() to get the video at the index of the listview (the farther down the longer, 54 being selecting the lastitem). So what am i doing wrong? or is it something with the that the IEnumerable is full of?

    heres is the entire code that I'm using. including a commented part where i was just testing the time to iterate the IEnumerable.
    Code:
     using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using BrightIdeasSoftware;
    using Google.GData.Client;
    using Google.GData.YouTube;
    using Google.YouTube;
    
    namespace YTManager
    {
        public partial class frmVideos : Form
        {
            private readonly mdiMain _parent;
            private readonly YouTubeRequest _ytRequest;
            //private List _videos = new List();
            private IEnumerable _videos;
            private int _videoCount;
    
            public frmVideos(string authToken, mdiMain parent)
            {
                InitializeComponent();
                _parent = parent;
                var settings = new YouTubeRequestSettings(mdiMain.APPLICATION_NAME, mdiMain.DEVELOPER_KEY);
                settings.AutoPaging = true;
                _ytRequest = new YouTubeRequest(settings);
                _ytRequest.Service.SetAuthenticationToken(authToken);
            }
    
            public void initVideos()
            {
                _parent.updateStatusBar("Loading your videos");
    
                Debug.WriteLine("Getting Videos: " + DateTime.Now);
                _videos = getVideos();
                Debug.WriteLine("Got Feeds: " + DateTime.Now);
    
                var stopWatch = new Stopwatch();
                /*
                try
                {
                    stopWatch.Start();
                    foreach (var video in _videos)
                    {
                        Debug.Print(video.Title);
                    }
                    stopWatch.Stop();
                }
                finally
                {
                    MessageBox.Show(stopWatch.ElapsedMilliseconds.ToString());
                
                }
                 * */
                //=======================//
                try
                {
                    Cursor = Cursors.WaitCursor;
                    stopWatch.Start();
                    oblstVideos.SetObjects(_videos);
                }
                finally
                {
                    stopWatch.Stop();
                    Cursor = Cursors.Default;
                }
                _parent.updateStatusBar(string.Format("SetObjects()time: {0} items in {1}ms", oblstVideos.Items.Count, stopWatch.ElapsedMilliseconds));
                
            }
    
            private static string checkNA(string value, string message)
            {
                return value == "-1" ? message : value;
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private IEnumerable getVideos()
            {
                Feed feed = null;
    
                try
                {
                    feed = _ytRequest.GetVideoFeed("default");
                    _videoCount = feed.TotalResults;
                }
                catch
                {
                    MessageBox.Show("Error while retrieving your videos!", "YT Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
    
                return feed != null ? feed.Entries : null;
            }
    
            private void oblstVideos_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                var player = new frmPlayer(_videos.ElementAt(oblstVideos.SelectedItem.Index));
                player.Show();
            }
    
            private void oblstVideos_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
            {
                var video = _videos.ElementAt(e.Item.Index);
    
                pcbThumbnail.ImageLocation = video.Thumbnails[0].Url;
                txtTitle.Text = video.Title;
                txtDescription.Text = video.Description;
                txtTags.Text = video.Keywords;
                chkPrivate.Checked = video.Private;
            }
    
        }
    }
    any help would be appreciated

    Thank You
     

Share This Page