Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:58:58+00:00 2026-06-04T00:58:58+00:00

I have to write a program in Matlab, the performance is quite important since

  • 0

I have to write a program in Matlab, the performance is quite important since it handles a lot of files.

I have to find the last file extension of a filename and split it there.

e.g. file.name.tar.gz should be split into file.name.tar and gz

I identified 3 methods to do this but I don’t know which is the fastest.

1.

javaFilename = java.lang.String(fileName);
lastDot = javaFilename.lastIndexOf('.');
name = char(javaFilename.substring(0, lastDot-1));    
ext = char(javaFilename.substring(lastDot+1));

2.

dots = findstr(fileName, '.');
lastDot = dots(length(dots));
name = fileName(1:lastDot-1);
ext = fileName(lastDot+1:end);    

3.

[name tempExt] = strtok(fileName, '.');
while tempExt
    [temp2 tempExt] = strtok(fileName, '.');
    if tempExt
       name = strcat(name, '.', temp2);
    end
end
ext = temp2(2:end);

I think the third one is the worst. But how about the other methods?
Can you come up with anything that is faster than the methods described by me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:58:59+00:00Added an answer on June 4, 2026 at 12:58 am

    You can test these by doing something like:

    tic;
    fileName = 'testFileName.ext';
    for i=1:100000
      <find last dot>
    end
    toc;
    

    I benchmarked yours and found the second one was much faster.

    My solution would be

    for index=length(fileName):-1:1
      if (fileName(index) == '.')
        break;
      end
    end
    

    The times I got were:

    Java Solution: 23 seconds

    findstr Solution: 0.4 seconds

    strtok Solution: (didn’t terminate; maybe I copied it wrong)

    reverse for-loop Solution: 0.01 seconds

    The advantage of my solution over findstr and strtok is that:
    a) it only looks for one dot, rather than all of them
    b) it starts from the end of the string (presumably most file extensions will be 2-4 letters long).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have the following read and write file program for an Array. How
I wanted to write a program that test if two files are duplicates (have
I have to write a program to find a rational number that has a
I have to write a program using this exact .h file not modifying anything.
I have to write a program that read from a file that contains the
I have a task to write a program which handles the division by zero
I have to write a program that sniffs network packets (part1-the simple part). And
I have to write a program in python where the user is given a
I have to write a program to check if any random string is present
I have to write a daemon program that constantly runs in the background and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.