Improving Performance of Unique Order Number Generation in Java

This article examines a Java utility for generating unique transaction order numbers, identifies performance inefficiencies caused by repeatedly creating SimpleDateFormat instances, proposes a static formatter optimization, presents benchmark code and results, and discusses alternative string concatenation methods for random code generation.

FunTester
FunTester
FunTester
Improving Performance of Unique Order Number Generation in Java

The original implementation creates a new SimpleDateFormat object on each call to createUniqueOrderNo(), which wastes memory and CPU time. The method also concatenates a timestamp with a random four‑digit code generated by getRandomLengthCode(int length).

/**
 * 生产唯一的交易订单号
 */
public static String createUniqueOrderNo() {
    SimpleDateFormat nyrsfm = new SimpleDateFormat("yyyyMMddHHmmss");
    return nyrsfm.format(new Date()) + getRandomLengthCode(4);
}

/**
 * 获取随机的短信验证码
 */
public static String getRandomLengthCode(int length) {
    return String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, length - 1)));
}

To improve performance, the SimpleDateFormat instance is made static, eliminating repeated object creation:

static SimpleDateFormat nyrsfm = new SimpleDateFormat("yyyyMMddHHmmss");

/**
 * 生产唯一的交易订单号
 */
public static String createUniqueOrderNo() {
    return nyrsfm.format(new Date()) + getRandomLengthCode(4);
}

A benchmark loop calls createUniqueOrderNo() 10,000 times and measures execution time. The optimized version shows a noticeable speed increase, as illustrated by the provided test result images.

The article also explores the random code generation method. Using string concatenation with the + operator creates additional objects, so String.valueOf() is preferred. A benchmark comparing the two approaches shows only a minor difference, indicating limited further optimization potential.

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    long nanoMark = getNanoMark();
    range(100000).forEach(x -> {
        // String ss = String.valueOf(1211);
        String ss = 1121 + ""; // alternative concatenation
    });
    output(getFormatNumber(getNanoMark() - nanoMark));
}

Overall, moving the date formatter to a static field significantly improves performance, while the random code generation method offers only marginal gains.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendperformanceCode Optimizationunique-order-id
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.