Results 1 to 6 of 6

Thread: efficiency

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    efficiency

    Hi

    I am trying to look for a more effecient, better way of doing this.

    Let's say we have a directory, in this directory there are various directories.

    Now, most of the directories will be a numeric/integer named directory ie:

    1
    2
    3
    65
    etc...

    however this directory may also contain non integer/numeric names (such as "test" or "h3ll0" etc...)

    What I want to do is to obtain a list of ONLY the integer directory name.

    Of course, I could use a for each on each directory name and check to see if the current Char in the current directory name is a digit/numeric or not but that i do not think is the most efficient way as there maybe several hundred directories therefore it will have some performance degration if using a Char.IsDigit/IsNumeric method.

    What is the best way?

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: efficiency

    I would use this code:
    VB Code:
    1. System.IO.Directory.GetDirectories("C:\MyPath\")
    To get all the directories and throw them into an array (You can use the AddRange() method of an ArrayList).

    Then run the IsNumeric() function on each string
    VB Code:
    1. public bool IsNumeric(string ToBeChecked)
    2.         {
    3.             double dbl = 0;
    4.             return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
    5.         }

    I think that's the best way to do it. You could try to check if they're numeric or not as you put them into an array or whatever you want.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: efficiency

    I used the following code to read in 704 directories and compare all of them:

    VB Code:
    1. Tester.frmTest.HiPerfTimer pt = new Tester.frmTest.HiPerfTimer();
    2. pt.Start();
    3. int numberOfDirs = 0;
    4. string dirBase = "K:\\Test\\";
    5. System.Collections.ArrayList dirs = new ArrayList(System.IO.Directory.GetDirectories(dirBase));
    6. foreach (string s in dirs)
    7. {
    8.     if (IsNumeric(s.Replace(dirBase, "")))
    9.     {
    10.         ++numberOfDirs;
    11.     }
    12. }
    13. pt.Stop();
    14. MessageBox.Show(pt.Duration.ToString());

    It took .012 seconds
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: efficiency

    Thanks for that guys

    the thing is - the method.TryParse() - if it cannot parse it, it would through an exception rather than a bool...

  5. #5
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: efficiency

    Quote Originally Posted by Techno
    Thanks for that guys

    the thing is - the method.TryParse() - if it cannot parse it, it would through an exception rather than a bool...
    You should be to handle that with a TRY...CATCH construct. Method documentation can give you details on the exceptions that are likely to be thrown too. If you look that up you can catch the exact exception rather than just a general exception as well.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: efficiency

    Quote Originally Posted by Techno
    Thanks for that guys

    the thing is - the method.TryParse() - if it cannot parse it, it would through an exception rather than a bool...
    The whole idea of the TryParse method(s) is that it doesn't throw an exception if the operation fails. If the method returns false then the string could not be parsed. If the method returns True then the parsed value is stored in the fourth argument, which is output only. I'd also be inclined to use NumberStyles.Integer for your purposes. Note that all primitive types have a TryParse method in .NET 2.0.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width