1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
| # cudf_performance_demo.py import time import numpy as np import pandas as pd import matplotlib.pyplot as plt import os
def create_test_data(num_rows=5_000_000): """创建测试数据集""" print(f"生成 {num_rows:,} 行测试数据...") np.random.seed(42) # 确保可重复性 data = { 'customer_id': np.random.randint(1, 10000, num_rows), 'product_id': np.random.randint(1, 500, num_rows), 'sales_amount': np.random.exponential(100, num_rows), 'quantity': np.random.randint(1, 10, num_rows), 'discount': np.random.uniform(0, 0.3, num_rows), 'region': np.random.choice(['North', 'South', 'East', 'West'], num_rows), 'category': np.random.choice(['Electronics', 'Clothing', 'Food', 'Books', 'Home'], num_rows), 'rating': np.random.randint(1, 6, num_rows), 'date': pd.date_range('2023-01-01', periods=num_rows, freq='T') } return data
def pandas_operations(data): """执行pandas操作""" print("\n🏁 开始Pandas测试...") # 创建DataFrame start_time = time.time() pdf = pd.DataFrame(data) creation_time = time.time() - start_time # 操作1: 基本统计 start_time = time.time() basic_stats = pdf.groupby('region').agg({ 'sales_amount': ['mean', 'sum', 'count'], 'quantity': ['mean', 'sum'], 'rating': 'mean' }) op1_time = time.time() - start_time # 操作2: 复杂过滤和计算 start_time = time.time() filtered_data = pdf[ (pdf['sales_amount'] > 50) & (pdf['rating'] >= 4) & (pdf['region'].isin(['North', 'South'])) ].copy() filtered_data['discounted_sales'] = filtered_data['sales_amount'] * (1 - filtered_data['discount']) filtered_data['sales_category'] = pd.cut(filtered_data['discounted_sales'], bins=[0, 50, 100, 200, float('inf')], labels=['Low', 'Medium', 'High', 'Very High']) op2_time = time.time() - start_time # 操作3: 时间序列分析 start_time = time.time() pdf['month'] = pdf['date'].dt.month monthly_sales = pdf.groupby(['month', 'category'])['sales_amount'].sum().unstack() op3_time = time.time() - start_time # 操作4: 多级分组和复杂聚合 start_time = time.time() customer_analysis = pdf.groupby(['customer_id', 'region']).agg({ 'sales_amount': ['sum', 'mean', 'count'], 'quantity': 'sum', 'rating': 'mean' }).round(2) customer_analysis.columns = ['_'.join(col).strip() for col in customer_analysis.columns.values] op4_time = time.time() - start_time total_time = creation_time + op1_time + op2_time + op3_time + op4_time return { 'creation': creation_time, 'basic_stats': op1_time, 'complex_filter': op2_time, 'time_series': op3_time, 'multi_group': op4_time, 'total': total_time }, basic_stats, filtered_data, monthly_sales, customer_analysis
def cudf_operations(data): """执行cuDF操作""" print("\n⚡ 开始cuDF测试...") try: import cudf # 创建DataFrame start_time = time.time() gdf = cudf.DataFrame(data) creation_time = time.time() - start_time # 操作1: 基本统计 start_time = time.time() basic_stats = gdf.groupby('region').agg({ 'sales_amount': ['mean', 'sum', 'count'], 'quantity': ['mean', 'sum'], 'rating': 'mean' }) op1_time = time.time() - start_time # 操作2: 复杂过滤和计算 start_time = time.time() filtered_data = gdf[ (gdf['sales_amount'] > 50) & (gdf['rating'] >= 4) & (gdf['region'].isin(['North', 'South'])) ].copy() filtered_data['discounted_sales'] = filtered_data['sales_amount'] * (1 - filtered_data['discount']) filtered_data['sales_category'] = cudf.cut(filtered_data['discounted_sales'], bins=[0, 50, 100, 200, float('inf')], labels=['Low', 'Medium', 'High', 'Very High']) op2_time = time.time() - start_time # 操作3: 时间序列分析 start_time = time.time() gdf['month'] = gdf['date'].dt.month monthly_sales = gdf.groupby(['month', 'category'])['sales_amount'].sum().unstack() op3_time = time.time() - start_time # 操作4: 多级分组和复杂聚合 start_time = time.time() customer_analysis = gdf.groupby(['customer_id', 'region']).agg({ 'sales_amount': ['sum', 'mean', 'count'], 'quantity': 'sum', 'rating': 'mean' }).round(2) customer_analysis.columns = ['_'.join(col).strip() for col in customer_analysis.columns.values] op4_time = time.time() - start_time total_time = creation_time + op1_time + op2_time + op3_time + op4_time return { 'creation': creation_time, 'basic_stats': op1_time, 'complex_filter': op2_time, 'time_series': op3_time, 'multi_group': op4_time, 'total': total_time }, basic_stats, filtered_data, monthly_sales, customer_analysis except ImportError: print("❌ cuDF未安装,跳过GPU测试") return None, None, None, None, None except Exception as e: print(f"❌ cuDF测试失败: {e}") return None, None, None, None, None
def verify_results(pandas_results, cudf_results): """验证pandas和cuDF结果的一致性""" if cudf_results is None: return print("\n🔍 验证结果一致性...") # 转换cuDF结果为pandas格式进行比较 cudf_basic_stats = cudf_results[1].to_pandas() if cudf_results[1] is not None else None cudf_filtered = cudf_results[2].to_pandas() if cudf_results[2] is not None else None cudf_monthly = cudf_results[3].to_pandas() if cudf_results[3] is not None else None cudf_customer = cudf_results[4].to_pandas() if cudf_results[4] is not None else None checks = [] # 检查基本统计 if cudf_basic_stats is not None and pandas_results[1] is not None: diff = np.abs(pandas_results[1] - cudf_basic_stats).max().max() checks.append(('基本统计', diff < 0.01)) # 检查过滤数据行数 if cudf_filtered is not None and pandas_results[2] is not None: row_diff = abs(len(pandas_results[2]) - len(cudf_filtered)) checks.append(('数据行数', row_diff == 0)) print("一致性检查结果:") for check_name, passed in checks: status = "✅ 通过" if passed else "❌ 失败" print(f" {check_name}: {status}")
def visualize_comparison(pandas_times, cudf_times): """可视化性能对比结果""" if cudf_times is None: print("无法生成图表:cuDF测试数据缺失") return operations = ['数据创建', '基本统计', '复杂过滤', '时间序列', '多级分组', '总计'] pandas_values = [ pandas_times['creation'], pandas_times['basic_stats'], pandas_times['complex_filter'], pandas_times['time_series'], pandas_times['multi_group'], pandas_times['total'] ] cudf_values = [ cudf_times['creation'], cudf_times['basic_stats'], cudf_times['complex_filter'], cudf_times['time_series'], cudf_times['multi_group'], cudf_times['total'] ] # 计算加速比 speedups = [pandas_values[i] / cudf_values[i] for i in range(len(operations))] # 创建图表 fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6)) # 图表1: 执行时间对比 x = np.arange(len(operations)) width = 0.35 bars1 = ax1.bar(x - width/2, pandas_values, width, label='Pandas (CPU)', color='#1f77b4', alpha=0.8) bars2 = ax1.bar(x + width/2, cudf_values, width, label='cuDF (GPU)', color='#ff7f0e', alpha=0.8) ax1.set_xlabel('操作类型', fontsize=12) ax1.set_ylabel('执行时间 (秒)', fontsize=12) ax1.set_title('Pandas vs cuDF 执行时间对比', fontsize=14, fontweight='bold') ax1.set_xticks(x) ax1.set_xticklabels(operations, rotation=45, ha='right') ax1.legend(fontsize=10) ax1.grid(axis='y', alpha=0.3) # 添加数值标签 for bar in bars1: height = bar.get_height() ax1.text(bar.get_x() + bar.get_width()/2., height + max(pandas_values + cudf_values)*0.01, f'{height:.3f}s', ha='center', va='bottom', fontsize=8) for bar in bars2: height = bar.get_height() ax1.text(bar.get_x() + bar.get_width()/2., height + max(pandas_values + cudf_values)*0.01, f'{height:.3f}s', ha='center', va='bottom', fontsize=8) # 图表2: 加速比 colors = ['green' if x >= 1 else 'red' for x in speedups] bars3 = ax2.bar(operations, speedups, color=colors, alpha=0.7) ax2.set_xlabel('操作类型', fontsize=12) ax2.set_ylabel('加速比 (Pandas/cuDF)', fontsize=12) ax2.set_title('cuDF GPU加速效果', fontsize=14, fontweight='bold') ax2.set_xticklabels(operations, rotation=45, ha='right') ax2.grid(axis='y', alpha=0.3) ax2.axhline(y=1, color='red', linestyle='--', alpha=0.5, label='基准线') ax2.legend() # 添加加速比数值 for i, (bar, speedup) in enumerate(zip(bars3, speedups)): height = bar.get_height() ax2.text(bar.get_x() + bar.get_width()/2., height + 0.1, f'{speedup:.1f}x', ha='center', va='bottom', fontsize=10, fontweight='bold') plt.tight_layout() # 保存图表 timestamp = int(time.time()) filename = f'cudf_performance_{timestamp}.png' plt.savefig(filename, dpi=300, bbox_inches='tight') print(f"\n📊 性能图表已保存: {filename}") plt.show()
def print_detailed_report(pandas_times, cudf_times): """打印详细性能报告""" print("\n" + "="*60) print("📈 详细性能报告") print("="*60) if cudf_times is None: print("只有Pandas测试结果:") for op, time_val in pandas_times.items(): print(f" {op:15}: {time_val:.4f}秒") return print(f"{'操作':15} | {'Pandas (秒)':>12} | {'cuDF (秒)':>10} | {'加速比':>8}") print("-" * 60) operations = [ ('creation', '数据创建'), ('basic_stats', '基本统计'), ('complex_filter', '复杂过滤'), ('time_series', '时间序列'), ('multi_group', '多级分组'), ('total', '总计') ] for op_key, op_name in operations: pandas_time = pandas_times[op_key] cudf_time = cudf_times[op_key] speedup = pandas_time / cudf_time print(f"{op_name:15} | {pandas_time:12.4f} | {cudf_time:10.4f} | {speedup:8.2f}x")
def main(): """主函数""" print("🚀 Pandas vs cuDF 性能对比演示") print("=" * 50) # 根据可用内存调整数据大小 try: import psutil available_memory = psutil.virtual_memory().available / (1024**3) # GB if available_memory < 8: num_rows = 2_000_000 # 200万行 print(f"检测到可用内存: {available_memory:.1f}GB,使用 {num_rows:,} 行数据") else: num_rows = 5_000_000 # 500万行 print(f"检测到可用内存: {available_memory:.1f}GB,使用 {num_rows:,} 行数据") except: num_rows = 3_000_000 # 默认300万行 print(f"使用默认 {num_rows:,} 行数据") # 生成测试数据 data = create_test_data(num_rows) # 执行测试 pandas_times, p_stats, p_filtered, p_monthly, p_customer = pandas_operations(data) cudf_times, c_stats, c_filtered, c_monthly, c_customer = cudf_operations(data) # 验证结果 verify_results((p_stats, p_filtered, p_monthly, p_customer), (c_stats, c_filtered, c_monthly, c_customer)) # 生成报告和图表 print_detailed_report(pandas_times, cudf_times) visualize_comparison(pandas_times, cudf_times) # 总结 print("\n" + "="*60) print("🎯 性能测试总结") print("="*60) if cudf_times is not None: total_speedup = pandas_times['total'] / cudf_times['total'] print(f"总体加速比: {total_speedup:.2f}x") if total_speedup > 1: print("✅ cuDF GPU加速效果显著!") else: print("⚠️ cuDF性能未达到预期,可能的原因:") print(" - 数据量太小,GPU优势不明显") print(" - 操作类型不适合GPU加速") print(" - GPU内存或计算资源限制") else: print("❌ 无法进行cuDF测试,请检查cuDF安装")
if __name__ == "__main__": main()
|