Java code for method invocation and performance issues

Source: Internet
Author: User

Recently doing performance analysis, using Eclipse's TPTP to do performance analysis, although the earlier known frequent call method is more cost-performance, after all, need to maintain a call stack, the specific theory has been blurred, take practice to narrate a problem.

Have the following code

/** * Get cache Define list * * @return list<wt_cachedomain>/public map<string, W T_cachedomain> Getcachecfgmap () {if (Cachecfgmap = null) {Cachecfgmap = new hashmap<string, WT_CACHEDOMAIN&G
			t; (); DEBUG if (getwt_cachelist ()!= null &&!getwt_cachelist (). IsEmpty ()) {for (int i = 0; i < Getwt_cach Elist (). Size ();
					i++) {Wt_cachedomain Cachedomain = (wt_cachedomain) getwt_cachelist (). get (i);
				Cachecfgmap.put (Cachedomain.getcache_name (), cachedomain);
	}} return cachecfgmap; }//This is to be taken from the cache and cannot be implemented from the database load public wt_cachedomain getcachewithname (String cachename) {//map<string, Wt_cachedomain> ;
		Cacheconfigmap = Getcachecfgmap (); if (CacheName = null | | "". Equals (cachename) | | Getcachecfgmap () = = NULL | |
		Getcachecfgmap (). IsEmpty ()) {return null;
		else {return Getcachecfgmap (). get (CacheName); }
	}
From the above code analysis, I used the Getcachecfgmap () three times in Getcachewithname (), so that a 300-time procedure with Getcachewithname () could be TPTP analyzed as follows:


You can see that the average performance of Getcachecfgmap () is 0.0001116 seconds

So I changed the code as follows:

Public map<string, Wt_cachedomain> Getcachecfgmap () {
		if (Cachecfgmap = = null) {
			Cachecfgmap = new hashmap& Lt String, wt_cachedomain> ();
			if (getwt_cachelist ()!= null &&!getwt_cachelist (). IsEmpty ()) {for
				(int i = 0; i < getwt_cachelist (). Size (); i++) {
					Wt_cachedomain Cachedomain = (wt_cachedomain) getwt_cachelist ()
							. get (i);
					Cachecfgmap.put (Cachedomain.getcache_name (), cachedomain);
				}
			}
		return cachecfgmap;
	}
	
	Public Wt_cachedomain getcachewithname (String cachename) {
		//Note I'm here to save the value of Getcachecfgmap () with a variable so that this method has only one call.
		map<string, wt_cachedomain> cacheconfigmap = Getcachecfgmap ();
		if (CacheName = null | | "". Equals (cachename)
				| | Cacheconfigmap = = NULL
				| | cacheconfigmap.isempty ()) {return
			null;
		} else {
			return Cacheconfigmap.get (CacheName);
		}
	
Note the section of the annotation so that getcachewithname () only invokes one cacheconfigmap (), and the same environment executes through TPTP analysis as follows:


Not only has the calls been reduced to 301 times, but the average execution time has also been reduced to 0.000007 times.

Then continue to modify the code as follows:

	Public map<string, Wt_cachedomain> Getcachecfgmap () {if (Cachecfgmap = = null) {Cachecfgmap = new hashmap<
			String, wt_cachedomain> (); if (getwt_cachelist ()!= null &&!getwt_cachelist (). IsEmpty ()) {for (int i = 0; i < getwt_cachelist (). Size ();
					i++) {Wt_cachedomain Cachedomain = (wt_cachedomain) getwt_cachelist (). get (i);
				Cachecfgmap.put (Cachedomain.getcache_name (), cachedomain);
	}} return cachecfgmap;
		Public Wt_cachedomain getcachewithname (String cachename) {//Note I am here to save the value of Getcachecfgmap () with a variable so that this method has only one call.
		map<string, wt_cachedomain> cacheconfigmap = Getcachecfgmap ();
		Cacheconfigmap = Getcachecfgmap ();
		Cacheconfigmap = Getcachecfgmap ();
		Cacheconfigmap = Getcachecfgmap ();
		Cacheconfigmap = Getcachecfgmap ();
		Cacheconfigmap = Getcachecfgmap ();
		Cacheconfigmap = Getcachecfgmap (); if (CacheName = null | | "". Equals (cachename) | | Cacheconfigmap = = NULL | | Cacheconfigmap.isempty ()) {REturn null;
		else {return cacheconfigmap.get (cachename); }
	}
Cacheconfigmap = Getcachecfgmap (); it was deliberately called six times, so whether the first code of performance would be poor. Keep looking at the TPTP results.


Was called 2,107 times, but the average time is much better than the first code, which means that Java performs the necessary optimizations for unwanted repetitive code, but still performs worse than the second piece of code.

Summarize:

1. If a method calls other methods more than once and its function is the same, get the value of the same case, use the temporary variable to save the value of the call, under pressure not only because of reduced number of calls to improve performance, but also to improve the performance of the called Method.

2. The greater the pressure that a method is invoked on itself, the worse its performance will be, whether there are 0 boundary points, unknown.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.