Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access Map at High Speed in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to access high-speed Map in Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

High-speed Map access

Using EnumMap to access Key is Enum, and it will be faster. Here is the status property of the object Result returned by a gateway, which is an enumerated class.

Public static enum Status {SUCCESS (1, "success"), FAIL (2, "processing failed"), DEGRADE (98, "successfully degraded"), UNKOWN (99, "unknown exception"); private int code; String msg; Status (int code,String msg) {this.code = code; this.msg = msg;} public int getCode () {return code } public String getMsg () {return msg;}}

Considering the definition of micro-service gateway return object, we should try to use java with its own type to avoid various serialization and deserialization problems. The gateway does not return this enumerated value, but returns the msg field, so we can construct an EnumMap,Key as the Status enumerated type and Value as the Status.msg property.

Map enumMap = null; private void initEnumMap () {enumMap = new EnumMap (Status.class); for (Status status:Status.values ()) {enumMap.put (status,status.msg);}}

When you construct EnumMap, you actually store all the enumerated values internally through an array, and the index is the enumerated ordinal. When you want to operate on EnumMap according to Enum, you just need to call ordinal to get its index, and then manipulate the array directly. Manipulating an one-dimensional array has the fastest speed.

In some scenarios, Key is of int type, so you can refer to IntMap in Chapter 2.

There are a lot of Key-Value scenarios that can be converted into accessing arrays according to the index. The C language we have learned operates on variable names, but in fact, it is still based on pointers to get the value in memory, Beetl template language, access to variables, unlike other scripting languages, access to Map through variable names, but the index value is assigned to this variable during compilation. All variables are stored in an one-dimensional array, and such access has more than ten times better performance than Map access.

The following scripting language

Var a = 1; var b = 2profa

Some language engines are translated into java code similar to the following

Context.put ("a", 1); context.put ("b", context.get ("a") + 2)

Here context is a Map. Access from Map via Key is fast, but Beetl sets the index of the array to the variables when parsing the scripting language, so the above script is translated in Beetl as follows

Object [] vars = context.vars; vars [0] = 1; vars [1] = vars [0] + 2

Here, the index in the variable table is set to 0 and 1 respectively for the variable aQuery b.

The basic component of an e-commerce has been optimized. The e-commerce system calls this component as many as 10 trillion times a day. This component is used to count the duration of method calls, collect it for a period of time, and then send it to the analysis system regularly. Used to find and analyze the performance of the method, some of the code is recorded by the duration of the call, the number of times the bar is used, and the following code

Watch watch = Watch.instance ("orderByWx"); / / initialize, orders from Wechat / / call other business logic. Profile.add (watch.endWatch ()); / / record once

The Watch class is defined as follows

Public class Watch {String key; long start; long millis =-1; private Watch (String key) {this.key = key; this.start = System.nanoTime ();} public static Watch instance (String key) {return new Watch (key);} public Watch endWatch () {millis = millisConsume (); return this } / * returns the milliseconds consumed by the method call * [@ return] (https://my.oschina.net/u/556800) * / private long millisConsume () {return TimeUnit.NANOSECONDS.toMillis (System.nanoTime ()-start);}}

The key attribute of Watch records the call type, such as order call, product query information, etc., which can be any value. The start attribute records the time point of the call, and millis records the number of milliseconds consumed by the call after calling endWatch.

The Profile class is used to record monitoring information and send it to the performance analysis center through other background threads. The example is simplified to present only the saved part.

Public class Profile {/ / call duration and number of calls static Map countMap = new ConcurrentHashMap (); / * * count the call time * [@ param] (https://my.oschina.net/u/2303379) watch * / public static void addWatch (Watch watch) {int consumeTime = (int) watch.millis; AtomicInteger count = countMap.get (consumeTime)) If (count==null) {count= new AtomicInteger (); AtomicInteger old = countMap.putIfAbsent (consumeTime,count); if (oldmilk null) {count= old;}} count.incrementAndGet ();}}

Profile initializes a ConcurrentHashMap for counting. Key is Integer, indicating the duration of the call. Value is AtomicInteger, which is used to count. Each call will be incremented by one.

Profile performance has a little room for optimization. From a business point of view, most of the methods or code blocks that need to be monitored do not take long to execute, assuming that the execution time is not more than 32 milliseconds (this is a hypothetical value, according to the statistical analysis of system operation and maintenance). Therefore, you can consider using a 32-length array to store all counts within 32 milliseconds, and those exceeding 32 milliseconds will follow the previous method.

Static Map countMap = new ConcurrentHashMap (); static final int MAX = 32; / / the number of calls that take 32 milliseconds to save: static AtomicInteger [] counts = new AtomicInteger [MAX]; static {for (int itemositi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report