Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I threw together a totally unoptimized program based on your discarded regex approach and timed it. It completed in 650 ms (250 ms with warmup). The slowest part doesn't involve the regex, but <code>String.format</code>. If we replace it with a straight <code>StringBuilder</code> approach, the time drops to 40 ms.</p> <pre><code>public class Test { static Pattern regex = Pattern.compile("(..)(..)(..)(..)(..)(..)"); public static void main(String[] args) { final List&lt;String&gt; inMacs = new ArrayList&lt;&gt;(), outMacs = new ArrayList&lt;&gt;(); for (int i = 0; i &lt; 80_000; i++) inMacs.add(mac()); final long start = System.nanoTime(); for (String mac : inMacs) { final Matcher m = regex.matcher(mac); m.matches(); outMacs.add(String.format("%s:%s:%s:%s:%s:%s", m.group(1), m.group(2), m.group(3), m.group(4), m.group(5), m.group(6))); } System.out.println("Took " + (System.nanoTime() - start)/1_000_000 + " milliseconds"); final Iterator&lt;String&gt; it = outMacs.iterator(); for (int i = 0; i &lt; 100; i++) System.out.println(it.next()); } static Random rnd = new Random(); static String mac() { final long mac = (long) (rnd.nextDouble()*(1L&lt;&lt;48)); return String.format("%012x", mac).toUpperCase(); } } </code></pre> <p>If you are really looking for a fast solution, then avoid the regex and use a simple test to detect your MAC format:</p> <pre><code> static List&lt;String&gt; fixMacs(List&lt;String&gt; inMacs) { final List&lt;String&gt; outMacs = new ArrayList&lt;&gt;(inMacs.size()); for (String mac : inMacs) outMacs.add( mac.charAt(2) == '-'? mac.replace("-", ":") : mac.charAt(2) != ':'? fixMac(mac) : mac); return outMacs; } static String fixMac(String inMac) { final StringBuilder b = new StringBuilder(18); for (int i = 0; i &lt; inMac.length(); i++) { b.append(inMac.charAt(i)); if (i%2 == 1 &amp;&amp; i != inMac.length()-1) b.append(':'); } return b.toString(); } </code></pre> <p>With this approach I measured just 8 ms for your 80,000 MACs.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload