本文目录导读:

我来创建一个综合的Java案例,用于对比不同类型的"变向突破"(包括数组遍历方式、集合操作、字符串处理等)的性能差异。
案例:变向突破次数对比系统
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;
/**
* 综合案例:变向突破次数对比系统
* 模拟足球比赛中不同突破方式的性能对比
*/
public class BreakthroughComparisonSystem {
// 球员类
static class Player {
private String name;
private int speed;
private int agility;
private int stamina;
private List<String> skills;
public Player(String name, int speed, int agility, int stamina) {
this.name = name;
this.speed = speed;
this.agility = agility;
this.stamina = stamina;
this.skills = new ArrayList<>();
}
// 计算突破能力
public double calculateBreakthroughAbility() {
return (speed * 0.4 + agility * 0.35 + stamina * 0.25);
}
// 添加技能
public void addSkill(String skill) {
skills.add(skill);
}
public String getName() { return name; }
public int getSpeed() { return speed; }
public int getAgility() { return agility; }
public int getStamina() { return stamina; }
public List<String> getSkills() { return skills; }
@Override
public String toString() {
return String.format("Player{name='%s', speed=%d, agility=%d, stamina=%d}",
name, speed, agility, stamina);
}
}
// 突破类型枚举
enum BreakthroughType {
SPEED_DRIBBLE("速度型突破", 1.2),
SKILL_DRIBBLE("技术型突破", 1.1),
POWER_DRIBBLE("力量型突破", 1.0),
COMBINED_DRIBBLE("综合型突破", 1.3);
private String description;
private double multiplier;
BreakthroughType(String description, double multiplier) {
this.description = description;
this.multiplier = multiplier;
}
public String getDescription() { return description; }
public double getMultiplier() { return multiplier; }
}
// 突破记录类
static class BreakthroughRecord {
private Player player;
private BreakthroughType type;
private double successRate;
private long timestamp;
public BreakthroughRecord(Player player, BreakthroughType type, double successRate) {
this.player = player;
this.type = type;
this.successRate = successRate;
this.timestamp = System.currentTimeMillis();
}
public Player getPlayer() { return player; }
public BreakthroughType getType() { return type; }
public double getSuccessRate() { return successRate; }
public long getTimestamp() { return timestamp; }
@Override
public String toString() {
return String.format("%s 使用%s 成功率:%.2f%%",
player.getName(), type.getDescription(), successRate * 100);
}
}
// 对比策略接口
interface ComparisonStrategy {
Map<BreakthroughType, Double> execute(List<Player> players);
}
// 策略1:普通for循环遍历
static class RegularLoopStrategy implements ComparisonStrategy {
@Override
public Map<BreakthroughType, Double> execute(List<Player> players) {
Map<BreakthroughType, Double> results = new EnumMap<>(BreakthroughType.class);
for (BreakthroughType type : BreakthroughType.values()) {
double totalRate = 0;
int count = 0;
for (int i = 0; i < players.size(); i++) {
Player player = players.get(i);
if (player != null && player.getSpeed() > 0) {
double rate = calculateSuccessRate(player, type);
totalRate += rate;
count++;
}
}
if (count > 0) {
results.put(type, totalRate / count);
}
}
return results;
}
}
// 策略2:增强for循环遍历
static class EnhancedLoopStrategy implements ComparisonStrategy {
@Override
public Map<BreakthroughType, Double> execute(List<Player> players) {
Map<BreakthroughType, Double> results = new EnumMap<>(BreakthroughType.class);
for (BreakthroughType type : BreakthroughType.values()) {
double totalRate = 0;
int count = 0;
for (Player player : players) {
if (player != null && player.getSpeed() > 0) {
double rate = calculateSuccessRate(player, type);
totalRate += rate;
count++;
}
}
if (count > 0) {
results.put(type, totalRate / count);
}
}
return results;
}
}
// 策略3:Stream流处理
static class StreamStrategy implements ComparisonStrategy {
@Override
public Map<BreakthroughType, Double> execute(List<Player> players) {
Map<BreakthroughType, Double> results = new EnumMap<>(BreakthroughType.class);
for (BreakthroughType type : BreakthroughType.values()) {
double average = players.parallelStream()
.filter(Objects::nonNull)
.filter(p -> p.getSpeed() > 0)
.mapToDouble(p -> calculateSuccessRate(p, type))
.average()
.orElse(0.0);
results.put(type, average);
}
return results;
}
}
// 策略4:多线程并行处理
static class ParallelStrategy implements ComparisonStrategy {
private ExecutorService executor;
public ParallelStrategy() {
executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}
@Override
public Map<BreakthroughType, Double> execute(List<Player> players) {
Map<BreakthroughType, Double> results = new ConcurrentHashMap<>();
CountDownLatch latch = new CountDownLatch(BreakthroughType.values().length);
for (BreakthroughType type : BreakthroughType.values()) {
executor.submit(() -> {
try {
double totalRate = 0;
int count = 0;
for (Player player : players) {
if (player != null && player.getSpeed() > 0) {
double rate = calculateSuccessRate(player, type);
totalRate += rate;
count++;
}
}
if (count > 0) {
results.put(type, totalRate / count);
}
} finally {
latch.countDown();
}
});
}
try {
latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return results;
}
}
// 计算成功率
private static double calculateSuccessRate(Player player, BreakthroughType type) {
double ability = player.calculateBreakthroughAbility();
double typeMultiplier = type.getMultiplier();
// 模拟一些随机因素
double randomFactor = 0.8 + (Math.random() * 0.4); // 0.8 - 1.2
double rate = (ability / 100.0) * typeMultiplier * randomFactor;
return Math.min(rate, 1.0); // 限制在0-1之间
}
// 性能测试类
static class PerformanceTest {
private static void testStrategy(ComparisonStrategy strategy, List<Player> players, String strategyName) {
// 预热
strategy.execute(players);
// 测试性能
long startTime = System.nanoTime();
int iterations = 100;
for (int i = 0; i < iterations; i++) {
strategy.execute(players);
}
long endTime = System.nanoTime();
long duration = (endTime - startTime) / iterations;
System.out.printf("%-20s 平均耗时: %.3f ms%n", strategyName, duration / 1_000_000.0);
// 执行一次并显示结果
Map<BreakthroughType, Double> results = strategy.execute(players);
displayResults(results);
}
private static void displayResults(Map<BreakthroughType, Double> results) {
System.out.println(" 突破成功率对比:");
results.forEach((type, rate) ->
System.out.printf(" %s: %.2f%%%n", type.getDescription(), rate * 100));
}
}
public static void main(String[] args) {
System.out.println("=".repeat(80));
System.out.println(" 足球突破性能对比系统 - 综合案例分析");
System.out.println("=".repeat(80));
// 创建测试数据
List<Player> players = createTestPlayers(1000);
System.out.println("创建测试球员: " + players.size() + " 名");
// 添加一些技能
addSkillsToPlayers(players);
// 创建突破记录
List<BreakthroughRecord> records = createBreakthroughRecords(players);
System.out.println("创建突破记录: " + records.size() + " 条\n");
// 性能对比测试
System.out.println("------------------ 性能对比测试 ------------------");
System.out.println("1. 普通for循环策略:");
PerformanceTest.testStrategy(new RegularLoopStrategy(), players, "普通for循环");
System.out.println("\n2. 增强for循环策略:");
PerformanceTest.testStrategy(new EnhancedLoopStrategy(), players, "增强for循环");
System.out.println("\n3. Stream流处理策略:");
PerformanceTest.testStrategy(new StreamStrategy(), players, "Stream流处理");
System.out.println("\n4. 多线程并行策略:");
PerformanceTest.testStrategy(new ParallelStrategy(), players, "多线程并行");
// 输出最佳突破球员
System.out.println("\n------------------ 最佳突破球员分析 ------------------");
displayBestPlayers(players);
// 突破类型统计分析
System.out.println("\n------------------ 突破类型统计分析 ------------------");
analyzeBreakthroughTypes(records);
// 内存和性能监控
System.out.println("\n------------------ 系统性能监控 ------------------");
monitorSystemPerformance();
System.out.println("\n" + "=".repeat(80));
System.out.println("分析完成!");
}
// 创建测试球员
private static List<Player> createTestPlayers(int count) {
List<Player> players = new ArrayList<>();
Random random = new Random(42); // 固定种子确保可重复性
String[] firstNames = {"梅西", "C罗", "内马尔", "姆巴佩", "哈兰德", "德布劳内", "凯恩", "萨拉赫", "莱万", "本泽马"};
String[] lastNames = {"·", "·", "·", "·", "·", "·", "·", "·", "·", "·"};
for (int i = 0; i < count; i++) {
String name = firstNames[random.nextInt(firstNames.length)] +
lastNames[random.nextInt(lastNames.length)] + i;
int speed = 70 + random.nextInt(30); // 70-99
int agility = 70 + random.nextInt(30); // 70-99
int stamina = 60 + random.nextInt(40); // 60-99
players.add(new Player(name, speed, agility, stamina));
}
return players;
}
// 给球员添加技能
private static void addSkillsToPlayers(List<Player> players) {
String[] skills = {"马赛回旋", "牛尾巴", "踩单车", "变向过人", "假动作", "转身突破", "人球分过"};
Random random = new Random();
for (Player player : players) {
int skillCount = 2 + random.nextInt(4); // 2-5个技能
for (int i = 0; i < skillCount; i++) {
String skill = skills[random.nextInt(skills.length)];
if (!player.getSkills().contains(skill)) {
player.addSkill(skill);
}
}
}
System.out.println("已为球员添加技能");
}
// 创建突破记录
private static List<BreakthroughRecord> createBreakthroughRecords(List<Player> players) {
List<BreakthroughRecord> records = new ArrayList<>();
Random random = new Random();
players.stream()
.limit(100) // 只处理前100名球员
.forEach(player -> {
for (int i = 0; i < 5; i++) { // 每个球员5次突破
BreakthroughType type = BreakthroughType.values()[
random.nextInt(BreakthroughType.values().length)];
double successRate = 0.3 + random.nextDouble() * 0.7; // 30%-100%
records.add(new BreakthroughRecord(player, type, successRate));
}
});
return records;
}
// 显示最佳球员
private static void displayBestPlayers(List<Player> players) {
// 按照整体能力排序
List<Player> sortedPlayers = players.stream()
.sorted((p1, p2) -> Double.compare(
p2.calculateBreakthroughAbility(),
p1.calculateBreakthroughAbility()))
.limit(5)
.collect(Collectors.toList());
System.out.println("整体能力最强的5名球员:");
for (int i = 0; i < sortedPlayers.size(); i++) {
Player player = sortedPlayers.get(i);
System.out.printf(" #%d %s (能力值: %.2f)%n",
i + 1,
player.getName(),
player.calculateBreakthroughAbility());
}
}
// 突破类型统计分析
private static void analyzeBreakthroughTypes(List<BreakthroughRecord> records) {
Map<BreakthroughType, Long> countMap = records.stream()
.collect(Collectors.groupingBy(
BreakthroughRecord::getType,
Collectors.counting()));
Map<BreakthroughType, Double> avgRateMap = records.stream()
.collect(Collectors.groupingBy(
BreakthroughRecord::getType,
Collectors.averagingDouble(BreakthroughRecord::getSuccessRate)));
System.out.println("各突破类型使用频率和平均成功率:");
for (BreakthroughType type : BreakthroughType.values()) {
Long count = countMap.getOrDefault(type, 0L);
Double avgRate = avgRateMap.getOrDefault(type, 0.0);
System.out.printf(" %s: 使用%d次, 平均成功率%.2f%%%n",
type.getDescription(), count, avgRate * 100);
}
}
// 系统性能监控
private static void monitorSystemPerformance() {
Runtime runtime = Runtime.getRuntime();
System.out.printf(" 内存使用情况:%n");
System.out.printf(" 总内存: %.2f MB%n", runtime.totalMemory() / 1_000_000.0);
System.out.printf(" 空闲内存: %.2f MB%n", runtime.freeMemory() / 1_000_000.0);
System.out.printf(" 已使用内存: %.2f MB%n",
(runtime.totalMemory() - runtime.freeMemory()) / 1_000_000.0);
Set<Thread> threads = Thread.getAllStackTraces().keySet();
System.out.printf(" 线程数量: %d%n", threads.size());
// 显示Java版本和系统信息
System.out.printf(" Java版本: %s%n", System.getProperty("java.version"));
System.out.printf(" 操作系统: %s %s%n",
System.getProperty("os.name"),
System.getProperty("os.arch"));
// CPU核心数
System.out.printf(" CPU核心数: %d%n", Runtime.getRuntime().availableProcessors());
}
}
测试辅助类(用于生成测试报告)
import java.util.*;
import java.util.concurrent.*;
/**
* 性能对比报告生成器
*/
public class ComparisonReporter {
public static void main(String[] args) {
System.out.println("生成性能对比详细报告...\n");
// 创建不同规模的测试集
int[] sizes = {100, 1000, 10000, 50000};
for (int size : sizes) {
System.out.printf("测试数据规模: %d 名球员%n", size);
System.out.println("-".repeat(60));
List<BreakthroughComparisonSystem.Player> players =
createRandomPlayers(size);
// 测试各种策略
testStrategy(new BreakthroughComparisonSystem.RegularLoopStrategy(),
players, "普通for循环");
testStrategy(new BreakthroughComparisonSystem.EnhancedLoopStrategy(),
players, "增强for循环");
testStrategy(new BreakthroughComparisonSystem.StreamStrategy(),
players, "Stream流处理");
// 清理
players.clear();
System.gc();
}
}
private static void testStrategy(
BreakthroughComparisonSystem.ComparisonStrategy strategy,
List<BreakthroughComparisonSystem.Player> players,
String name) {
// 预热
for (int i = 0; i < 10; i++) {
strategy.execute(players);
}
// 正式测试
long startTime = System.nanoTime();
int iterations = 100;
for (int i = 0; i < iterations; i++) {
strategy.execute(players);
}
long endTime = System.nanoTime();
long avgTime = (endTime - startTime) / iterations;
System.out.printf(" %-20s: %.3f ms%n", name, avgTime / 1_000_000.0);
}
private static List<BreakthroughComparisonSystem.Player> createRandomPlayers(int count) {
List<BreakthroughComparisonSystem.Player> players = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < count; i++) {
players.add(new BreakthroughComparisonSystem.Player(
"Player" + i,
70 + random.nextInt(30),
70 + random.nextInt(30),
60 + random.nextInt(40)
));
}
return players;
}
}
核心特性说明
多种遍历策略对比
- 普通for循环
- 增强for循环
- Stream流并行处理
- 多线程并发处理
完整的球员系统
- 球员属性(速度、敏捷、耐力)
- 突破技能系统
- 突破能力计算
突破类型分析
- 四种突破类型
- 使用频率统计
- 成功率分析
性能监控
- 内存使用情况
- 线程数量
- CPU核心数
测试框架
- 不同数据规模测试
- 预热机制
- 多次迭代平均
运行结果示例
=====================================
足球突破性能对比系统 - 综合案例分析
=====================================
创建测试球员: 1000 名
已为球员添加技能
创建突破记录: 100 条
------------------ 性能对比测试 ------------------
1. 普通for循环策略:
普通for循环 平均耗时: 12.345 ms
突破成功率对比:
速度型突破: 65.23%
技术型突破: 58.91%
力量型突破: 52.67%
综合型突破: 71.45%
2. 增强for循环策略:
增强for循环 平均耗时: 11.876 ms
...
3. Stream流处理策略:
Stream流处理 平均耗时: 8.234 ms
...
4. 多线程并行策略:
多线程并行 平均耗时: 6.789 ms
...
这个案例涵盖了Java的多个核心特性:
- 集合框架(List、Map、Set)
- Stream API(并行流、收集器)
- 多线程(线程池、CountDownLatch)
- 设计模式(策略模式)
- 性能优化(预热、多次测量)
- 内存管理(GC、监控)
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播