电脑:这才是世界上最绝望的死法
2025-02-19 19:32:58
发布于:江苏
注:所有代码均有AI书写,本人一概不负责(AI预估CPU会烧到95摄氏度,慎用!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <iostream>
#include <vector>
#include <thread>
#include <atomic>
#include <Windows.h>
#include <immintrin.h> // SIMD指令
using namespace std;
constexpr int W = 120; // 控制台宽度
constexpr int H = 40; // 控制台高度
constexpr int NUM_PARTICLES = 500000; // 粒子数量
constexpr float GRAVITY = 0.0005f; // 重力加速度
// 内存对齐结构体 (32字节对齐)
struct alignas(32) Particle {
__m256 pos_x; // 8个粒子的X坐标
__m256 pos_y; // 8个粒子的Y坐标
__m256 vel_x;
__m256 vel_y;
__m256 color;
};
vector<Particle*> particles; // 粒子数组
atomic<bool> running{true};
// SIMD重力计算
void apply_gravity(__m256& vel_y) {
vel_y = _mm256_add_ps(vel_y, _mm256_set1_ps(GRAVITY));
}
// 多线程更新函数
void update_thread(int start, int end) {
const __m256 bounds_x = _mm256_set1_ps(W-1);
const __m256 bounds_y = _mm256_set1_ps(H-1);
const __m256 damp = _mm256_set1_ps(-0.7f);
for (int i = start; i < end; ++i) {
Particle& p = *particles[i];
// 应用重力
apply_gravity(p.vel_y);
// 更新位置
p.pos_x = _mm256_add_ps(p.pos_x, p.vel_x);
p.pos_y = _mm256_add_ps(p.pos_y, p.vel_y);
// 边界碰撞检测
__m256 mask_x = _mm256_cmp_ps(p.pos_x, bounds_x, _CMP_GT_OQ);
p.pos_x = _mm256_blendv_ps(p.pos_x, bounds_x, mask_x);
p.vel_x = _mm256_blendv_ps(p.vel_x, _mm256_mul_ps(p.vel_x, damp), mask_x);
__m256 mask_y = _mm256_cmp_ps(p.pos_y, bounds_y, _CMP_GT_OQ);
p.pos_y = _mm256_blendv_ps(p.pos_y, bounds_y, mask_y);
p.vel_y = _mm256_blendv_ps(p.vel_y, _mm256_mul_ps(p.vel_y, damp), mask_y);
}
}
void render(HANDLE hConsole) {
// 使用内存映射直接操作控制台缓冲区
CHAR_INFO buffer[H][W] = {0};
for (auto& p : particles) {
float* px = (float*)&p->pos_x;
float* py = (float*)&p->pos_y;
float* clr = (float*)&p->color;
for (int i = 0; i < 8; ++i) { // 处理每个SIMD包中的8个粒子
int x = min(max((int)px[i], 0), W-1);
int y = min(max((int)py[i], 0), H-1);
buffer[y][x].Char.UnicodeChar = 0x2588;
buffer[y][x].Attributes = (int)(clr[i] * 255) % 0xFF;
}
}
// 批量写入控制台
COORD size = {W, H};
COORD coord = {0, 0};
SMALL_RECT rect = {0, 0, W-1, H-1};
WriteConsoleOutput(hConsole, (CHAR_INFO*)buffer, size, coord, &rect);
}
int main() {
HANDLE hConsole = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
// 预分配内存(对齐内存)
const int num_blocks = (NUM_PARTICLES + 7) / 8;
particles.resize(num_blocks);
for (auto& p : particles) {
p = (Particle*)_aligned_malloc(sizeof(Particle), 32);
// 初始化SIMD数据...
}
// 启动多个工作线程
const int num_threads = thread::hardware_concurrency();
vector<thread> threads;
int per_thread = num_blocks / num_threads;
while (running) {
auto start = chrono::high_resolution_clock::now();
// 并行更新
for (int i = 0; i < num_threads; ++i) {
int start = i * per_thread;
int end = (i == num_threads-1) ? num_blocks : start + per_thread;
threads.emplace_back(update_thread, start, end);
}
// 等待所有线程完成
for (auto& t : threads) t.join();
threads.clear();
// 渲染
render(hConsole);
// 限帧
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
if (duration.count() < 33) {
this_thread::sleep_for(chrono::milliseconds(33 - duration.count()));
}
}
// 清理内存
for (auto p : particles) _aligned_free(p);
return 0;
}
全部评论 1
2025-02-20 来自 北京
0报错吗?找AI改
2025-02-20 来自 江苏
0AI越改越乱,本来只是一个WA结果最后全TLE(讯飞星火官方代码修改大师现状)
2025-02-20 来自 北京
0本来我跟他说得就是挑战极限,CPU别烧糊了就行
2025-02-20 来自 江苏
0
有帮助,赞一个