Skip to content

Commit

Permalink
Faster string formatting for spectrum name string operations
Browse files Browse the repository at this point in the history
This commit results in a 69% speedup to PTMShepherd.reNormName
  • Loading branch information
danielgeiszler committed Apr 12, 2024
1 parent 7f4ccb5 commit 8d7c324
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/edu/umich/andykong/ptmshepherd/PTMShepherd.java
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,18 @@ public static String reNormName(String s) {
String[] sp = s.split("\\.");
if (sp[sp.length-2].equals(sp[sp.length-1])) // If the format is XXX.Spec.Spec already, return input string
return s;
int sn = Integer.parseInt(sp[1]);
//without charge state
return String.format("%s.%d.%d", sp[0], sn, sn);

// Remove leading zeros from the string
String snString = sp[1].replaceFirst("^0+", "");

// Using StringBuilder for potentially better performance
StringBuilder sb = new StringBuilder();
sb.append(sp[0]);
sb.append('.');
sb.append(snString);
sb.append('.');
sb.append(snString);
return sb.toString();
}

public static String reNormNameWithCharge(String s) {
Expand Down
15 changes: 15 additions & 0 deletions test/PTMShepherdTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import edu.umich.andykong.ptmshepherd.PTMShepherd;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class PTMShepherdTest {

@Test
void reNormName() {
String specName = "filename.0001.0001.3";
String expectedSpecName = "filename.1.1";
String reNormedName = PTMShepherd.reNormName(specName);
System.out.println(reNormedName);
assertEquals(expectedSpecName, reNormedName);
}
}

0 comments on commit 8d7c324

Please sign in to comment.