测试代码

package com.mx2913.jpcc.sample;

import java.util.Locale;

public class test {

    public static boolean isJsp1(String t) {
        char c;
        int end = t.length() - 1;

        if ((end > 3) && ((c = t.charAt(end)) == 'x' || c == 'X')) {
            end--;
        }

        if ((end > 2) && ((c = t.charAt(end)) == 'p' || c == 'P')) {
            end--;
            if ((c = t.charAt(end)) == 's' || c == 'S') {
                end--;
                if ((c = t.charAt(end)) == 'j' || c == 'J') {
                    end--;
                    return t.charAt(end) == '.';
                }
            }
        }

        return false;
    }

    public static boolean isJsp2(String t) {
        return t.toLowerCase(Locale.ROOT).endsWith(".jspx") || t.toLowerCase(Locale.ROOT).endsWith(".jsp");
    }

    public static boolean isJsp3(String t) {
        char c;
        int end = t.length() - 1;
        if (end < 3) {
            return false;
        }
        if ((c = t.charAt(end)) == 'x' || c == 'X') {
            --end;
        }
        if ((c = t.charAt(end)) != 'p' && c != 'P') {
            return false;
        }
        --end;
        if ((c = t.charAt(end)) != 's' && c != 'S') {
            return false;
        }
        --end;
        if ((c = t.charAt(end)) != 'j' && c != 'J') {
            return false;
        }
        --end;
        return t.charAt(end) == '.';
    }

    public static void main(String[] args) {
        final String[] urls = {".sp", "1.spx", "1.jspx", "localhost/.jsp", "localhost/x.p", "localhost/x.x", "localhost/x.jsp", "localhost/x.jspx"};
        long times = 1;
        for (String url : urls) {
            System.out.println("Test Url: " + url);
            System.out.println("Result Test: ");
            System.out.println("isJsp1 => " + isJsp1(url));
            System.out.println("isJsp2 => " + isJsp2(url));
            System.out.println("isJsp3 => " + isJsp3(url));
            System.out.println("Performance Test: ");
            runCommand("isJsp1 => " + times + " 次", () -> {
                for (long i = 0; i < times; i++) isJsp1(url);
            });
            runCommand("isJsp2 => " + times + " 次", () -> {
                for (long i = 0; i < times; i++) isJsp2(url);
            });
            runCommand("isJsp3 => " + times + " 次", () -> {
                for (long i = 0; i < times; i++) isJsp3(url);
            });
        }
        System.out.println();
    }

    public static void runCommand(String name, Runnable runnable) {
        System.out.println("执行任务:" + name);
        long start = System.nanoTime();
        runnable.run();
        System.out.println("执行时间: " + (System.nanoTime() - start) + " ns");
    }
}

测试结果

Test Url: .sp
Result Test: 
isJsp1 => false
isJsp2 => false
isJsp3 => false
Performance Test: 
执行任务:isJsp1 => 1 次
执行时间: 10017 ns
执行任务:isJsp2 => 1 次
执行时间: 6189 ns
执行任务:isJsp3 => 1 次
执行时间: 3287 ns
Test Url: 1.spx
Result Test: 
isJsp1 => false
isJsp2 => false
isJsp3 => false
Performance Test: 
执行任务:isJsp1 => 1 次
执行时间: 1415 ns
执行任务:isJsp2 => 1 次
执行时间: 2286 ns
执行任务:isJsp3 => 1 次
执行时间: 1105 ns
Test Url: 1.jspx
Result Test: 
isJsp1 => true
isJsp2 => true
isJsp3 => true
Performance Test: 
执行任务:isJsp1 => 1 次
执行时间: 1249 ns
执行任务:isJsp2 => 1 次
执行时间: 2270 ns
执行任务:isJsp3 => 1 次
执行时间: 1690949 ns
Test Url: localhost/.jsp
Result Test: 
isJsp1 => true
isJsp2 => true
isJsp3 => true
Performance Test: 
执行任务:isJsp1 => 1 次
执行时间: 1459 ns
执行任务:isJsp2 => 1 次
执行时间: 2673 ns
执行任务:isJsp3 => 1 次
执行时间: 1198 ns
Test Url: localhost/x.p
Result Test: 
isJsp1 => false
isJsp2 => false
isJsp3 => false
Performance Test: 
执行任务:isJsp1 => 1 次
执行时间: 1050 ns
执行任务:isJsp2 => 1 次
执行时间: 2538 ns
执行任务:isJsp3 => 1 次
执行时间: 1063 ns
Test Url: localhost/x.x
Result Test: 
isJsp1 => false
isJsp2 => false
isJsp3 => false
Performance Test: 
执行任务:isJsp1 => 1 次
执行时间: 1082 ns
执行任务:isJsp2 => 1 次
执行时间: 2638 ns
执行任务:isJsp3 => 1 次
执行时间: 1004 ns
Test Url: localhost/x.jsp
Result Test: 
isJsp1 => true
isJsp2 => true
isJsp3 => true
Performance Test: 
执行任务:isJsp1 => 1 次
执行时间: 1181 ns
执行任务:isJsp2 => 1 次
执行时间: 2662 ns
执行任务:isJsp3 => 1 次
执行时间: 1135 ns
Test Url: localhost/x.jspx
Result Test: 
isJsp1 => true
isJsp2 => true
isJsp3 => true
Performance Test: 
执行任务:isJsp1 => 1 次
执行时间: 1177 ns
执行任务:isJsp2 => 1 次
执行时间: 1967 ns
执行任务:isJsp3 => 1 次
执行时间: 1062 ns

大部分情况下,性能优化比较高