E
E
Eugene2014-07-11 10:48:59
.NET
Eugene, 2014-07-11 10:48:59

How will it be faster to get the file extension?

How will it be faster:

string ext = System.IO.Path.GetExtension(FileName);

or
string ext=FileName.Substring(FileName.LastIndexOf('.'));

or
System.IO.FileInfo fi = new System.IO.FileInfo(FileName);
string ext = fi.Extension;

or
string[] temp= FileName.Split('.');
string ext =temp[temp.Length-1];

or
System.Text.RegularExpressions.Regex extend = new System.Text.RegularExpressions.Regex(@"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value;

=)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
C
Cyril, 2014-07-11
@llirikkkk

c.w.e - conditional time unit XD
1 place (123 c.w.e):
2nd place (315 c.u.e.):
3rd place (519 c.u.e.):

string[] temp= FileName.Split('.');
string ext =temp[temp.Length-1];

4th place (7254 c.u.e.):
System.IO.FileInfo fi = new System.IO.FileInfo(FileName);
string ext = fi.Extension;

5th place (14341 c.u.e.):
System.Text.RegularExpressions.Regex extend = new System.Text.RegularExpressions.Regex(@"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value;

A
Alexander Taratin, 2014-07-11
@Taraflex

string ext=FileName.Substring(FileName.LastIndexOf('.'));
string[] temp= FileName.Split('.');
string ext =temp[temp.Length-1];
System.Text.RegularExpressions.Regex extend = new System.Text.RegularExpressions.Regex(@"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value; Some_path/fome_folder_with.dot/some_file_without_extention or some_path/.some_name_started_with_dot
do not take into account such a case . The latter files cannot be created in Windows but can be used, many cross-platform programs sin by creating such "foreign" paths.

A
Alexey Zenkov, 2014-07-11
@Lexxus31337

add another option

Regex extend = new Regex(@"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value

E
Eugene, 2014-07-11
@gudus

Tested):
System.IO.Path.GetExtension: ext=.txt milSec=00:00:00.0000016
Substring: ext=.txt milSec=00:00:00.0000019
System.IO.FileInfo: ext=.txt milSec=00:00 :00.0000445
Split: ext=txt milSec=00:00:00.0000009
System.Text.RegularExpressions.Regex: ext=txt milSec=00:00:00.0004294
=)

A
Alexey Pavlov, 2014-07-11
@lexxpavlov

What do you use for? I mean, how often will it run? If it is rare, for example, on a mouse click, then it does not matter at all how long this operation takes, and the clarity of the command will be more important in terms of further support. Therefore, Path.GetExtension() is the best choice.
And if you have this command called many times in a cycle, and its performance is really important, only then you should think about how to get data.
The general rule is to write as clear and obvious code as possible, and where performance is needed, only there you need to optimize. Premature optimization is evil, because it takes a lot of programmer time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question