Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to process an audio library file which is about 350 KB. My application processes each song on the file gets its meta-data and displays it in a List Box.

The problem is that it takes too much time (about 1-2 mins). I need to make it faster like in the Windows Media Player. I tried to make it faster by saving meta data for each song in the library file. Now it takes about 30-50 secs. The application works fine; no performance issues.

If I use this method then it takes 1 min to load and process:

    public void m3uplaylist()
    {


            try
            {
                song.Clear();
            }
            catch (Exception)
            {
            }

        string fileName = null;
        FileStream fStream = null;
        StreamReader sReader = null;
        if (File.Exists(filename))
        {
            fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            sReader = new StreamReader(fStream);

            while ((fileName = sReader.ReadLine()) != null)
            {
                if (fileName.Length > 0 && fileName.Substring(0, 1) != "#" && fileName.Substring(0, 1) != "\n") //Checks whether the first character of the line is not # or Enter
                {
                    try
                    {
                        string[] row1 = { fileName.Substring(0, fileName.LastIndexOf("\\")) + "\\" + fileName.Substring(fileName.LastIndexOf("\\") + 1) }; //Stores the song details in string array so that it can be added to the Grid



                        for (int i = 0; i < row1.Length; i++)
                        {

                            Songss sing = new Songss(row1[i]);
                            song.Add(sing);
                            progress++;
                            workers.ReportProgress(progress, bar1);

                        }
                        TotalSongs = File.ReadAllText(filename).Length / 100;

                    }
                    catch (FileNotFoundException ex)
                    {

                    }

                }

            }
            fStream.Close();
            sReader.Close();
        }

    }

If I use this method the library loads in about 15 seconds:

    public void mplplaylistload(string filename)
    {

        if (File.Exists(filename))
        {
            TotalSongs = File.ReadAllText(filename).Length / 100 - 500;
            string file = System.IO.Path.GetFileNameWithoutExtension(filename);
            // Create the query 
            var rowsFromFile = from c in XDocument.Load(
                                filename).Elements(
                                "playlist").Elements("abc123").Elements("Song")
                               select c;
            // Execute the query 
            foreach (var row in rowsFromFile)
            {
                Songss sing = new Songss(row.Element("Path").Value);

                    sing.title = row.Element("Title").Value;
                    if (GetFullInfo == true)
                    {
                        sing.album = row.Element("Album").Value;

                        sing.artist = row.Element("Artist").Value;

                        sing.length = row.Element("Length").Value;

                        sing.size = row.Element("Size").Value;

                        if (File.Exists(row.Element("Image").Value))
                        {
                            BitmapImage img = new BitmapImage(new Uri(row.Element("Image").Value));
                            img.CacheOption = BitmapCacheOption.None;
                            sing.source = img;
                        }
                    }
                song.Add(sing);
                progress++;

                workers.ReportProgress(progress);
            }

        }

    }

The file from which it reads is like this:

   <?xml version="1.0" encoding="utf-8"?>
   <playlist>
   <abc123>
   <Song>
  <Title>9</Title>
  <Path>D:\MuziK\Music\9.mp3</Path>
  <Album>Unkown Album</Album>
  <Artist>Unknown Artist</Artist>
  <Size>2,960KB</Size>
  <Length>NA</Length>
  <Image>C:\Users\The GaMeR\AppData\Local\Macalifa\albumarts\Unkown Album\9\art.jpg</Image>
</Song>
<Song>
  <Title>Aa Bhi Ja Sanam</Title>
  <Path>D:\MuziK\Music\Aa Bhi Ja Sanam.mp3</Path>
  <Album></Album>
  <Artist></Artist>
  <Size>2,941KB</Size>
  <Length>3:08</Length>
  <Image>C:\Users\The GaMeR\AppData\Local\Macalifa\albumarts\\Aa Bhi Ja Sanam\art.jpg</Image>
</Song>
 </abc123>


What I need is to optimize the time-taken by the library to load.

Note: I am using background worker to load the library. Is there any better approach than this?

share|improve this question
1  
Can you clarify a little about your two "approaches"? From a first glance they don't seem to do the same, why exactly are they considered an alternative? And since you know one of them is 4 times faster than the other, why do you want a codereview from that slower one? – Jeroen Vannevel Apr 25 '14 at 0:58
    
@JeroenVannevel The first approach is slow, because i think it fetches meta-data for each song. The second however, is more faster because meta-data for each song is saved in the library file. I don't want codereview from slower one, i want code review of the faster one, to make it even more faster. As i have mentioned in my question, that i want it to be just like WMP, When user opens the application the library should already be loaded..... The Don't look the same of-course.. The first one only reads the "Song Paths" but the second one reads everything. – TheGaMeR123 Apr 25 '14 at 11:07

1 Answer 1

Instead of loading the entire XML file, you can use XStreamingElement and xmlReader to reduce the memory foot print. As your playlist grows, large xml file will become a problem. The following MSDN link describes it http://msdn.microsoft.com/en-us/library/bb387013.aspx

share|improve this answer
    
Thanks!! i will look into it & inform you if it solves my problem – TheGaMeR123 Apr 25 '14 at 11:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.