How can i stress my phone's CPU programatically?

Mohammad Adib picture Mohammad Adib · Sep 13, 2011 · Viewed 7k times · Source

So i overclocked my phone to 1.664ghz and I know there are apps that test your phone's CPU performance and stressers but I would like to make my own someway. What is the best way to really make your CPU work? I was thinking just making a for loop do 1 million iterations of doing some time-consuming math...but that did not work becuase my phone did it in a few milliseconds i think...i tried trillions of iterations...the app froze but my task manager did not show the cpu even being used by the app. Usually stress test apps show up as red and say cpu:85% ram: 10mb ...So how can i really make my processor seriously think?

Answer

user244343 picture user244343 · Sep 13, 2011

To compile a regex string:

Pattern p1 = Pattern.compile("a*b"); // a simple regex
// slightly more complex regex: an attempt at validating email addresses
Pattern p2 = Pattern.compile("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b");

You need to launch these in background threads:

class RegexThread extends Thread {
   RegexThread() {
      // Create a new, second thread
      super("Regex Thread");
      start(); // Start the thread
   } 

   // This is the entry point for the second thread.
   public void run() {
      while(true) {
        Pattern p = Pattern.compile("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b");
      }
   }
}

class CPUStresser {
   public static void main(String args[]) {
      static int NUM_THREADS = 10, RUNNING_TIME = 120; // run 10 threads for 120s
      for(int i = 0; i < NUM_THREADS; ++i) {
         new RegexThread(); // create a new thread
      }
      Thread.sleep(1000 * RUNNING_TIME);
   }
}

(above code appropriated from here)

See how that goes.