DEV Community

Cover image for My notes on Android optimization. Part 2. Quick example of benchmarking Kotin regular expressions
Tristan Elliott
Tristan Elliott

Posted on

My notes on Android optimization. Part 2. Quick example of benchmarking Kotin regular expressions

Resources

  • Mastering Regular expressions by Jeffrey Fried. Chapter 6

My app on the Google play store

My app's GitHub code

Example

  • I create a for loop to warm up the JVM and allow the compiler to start optimizing (This mimics a real world JVM and doing this we will get times 30% faster). start a timer, run my regular expression code 1000 times, end the timer. We do that four times and take the average. Which looks like this :
    @Test
    fun testing_again_test2(){

        val timesToDo = 1000L
        val channelName = "cohhcarnage"


        val longerParsingString = "real world example of what you want to parse"

// for loop to `warm up the JVM`
        for (i in 4 downTo 1) {
            var count = timesToDo
            val startTime = System.currentTimeMillis()
            while (--count > 0) {


  underTest.privateMessageParsing(longerParsingString,channelName) 
//privateMessageParsing() contains the matching actions we are bench marking. This is where you would substitute your own code.
            }
            val seconds = (System.currentTimeMillis() - startTime) / 1000.0
            println("Alternation takes $seconds seconds")
        }

    }

Enter fullscreen mode Exit fullscreen mode
  • my next blog post will be how I was able to improve my regular expressions(this involves reading a lot of technical documentation )

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)