[TR]
Bu proje, .NET Core uygulamalarında Span<T>
ve Memory<T>
kullanımının performans avantajlarını göstermektedir. Bu tipler, diziler ve stringlerle çalışırken yüksek performanslı ve düşük bellek ayırma alternatifleri sunar.
Benchmark testleri, geleneksel yaklaşımlar ile Span-tabanlı yaklaşımların karşılaştırmalı performans sonuçlarını göstermektedir:
BenchmarkDotNet v0.14.0, Windows 11, Intel Core i7-1360P
.NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
| İşlem | Ortalama Süre | Bellek Kullanımı |
|-------------------------|---------------|------------------|
| Geleneksel String | 36.238 μs | 111,200 bytes |
| Span String | 24.023 μs | 79,920 bytes |
| Geleneksel Dizi Kopyala | 118.4 ns | 0 bytes |
| Span Dizi Kopyala | 110.4 ns | 0 bytes |
| Geleneksel Sayı Ayrıştır| 18.465 μs | 39,944 bytes |
| Span Sayı Ayrıştır | 10.022 μs | 0 bytes |
Önemli Gözlemler:
-
String işlemlerinde Span kullanımı:
- %33 daha hızlı çalışma
- %28 daha az bellek kullanımı
-
Dizi kopyalama işlemlerinde:
- %7 performans artışı
- Her iki yöntemde de ek bellek kullanımı yok
-
Sayı ayrıştırma işlemlerinde:
- %45 daha hızlı çalışma
- Sıfır ek bellek kullanımı
-
SpanMemoryDemo.Core: Geleneksel ve Span-tabanlı yaklaşımları gösteren örnek uygulamaları içerir
StringParsingExample.cs
: Bellek ayırma yapmadan verimli string ayrıştırma örnekleriArrayOperationsExample.cs
: Bellek açısından verimli dizi işlemleri
-
SpanMemoryDemo.Benchmarks: Geleneksel ve Span-tabanlı yaklaşımların karşılaştırmalı performans testlerini içerir
SpanBenchmarks.cs
: Çeşitli işlemler için karşılaştırmalı performans testleri
-
String Ayrıştırma
- Geleneksel string bölme vs Span-tabanlı ayrıştırma
- Stringlerden bellek-verimli sayı ayrıştırma
-
Dizi İşlemleri
- Verimli dizi kopyalama
- Stack-tabanlı dizi işlemleri
- Bellek ayırma yapmadan dizi segment işlemleri
-
Performans Testleri
- Bellek ayırma karşılaştırmaları
- Çalışma süresi karşılaştırmaları
- Projeyi Release modunda derleyin
- SpanMemoryDemo.Benchmarks dizinine gidin
- Şu komutu çalıştırın:
dotnet run -c Release
-
Azaltılmış Bellek Ayırmaları
- Ayrıştırma sırasında string ayırmalarından kaçınma
- Kopya oluşturmadan dizi segmentleriyle çalışma
-
Geliştirilmiş Performans
- Daha az çöp toplama baskısı
- Daha verimli bellek kullanımı
- Daha iyi önbellek kullanımı
-
Stack Ayırma
- Stack-ayrılmış dizilerle çalışabilme
- Geçici tamponlar için heap ayırmalarından kaçınma
- Stack-only işlemler için
Span<T>
kullanın - Referansı saklamanız gerektiğinde
Memory<T>
kullanın - Veriyi değiştirmeniz gerekmediğinde
ReadOnlySpan<T>
'yi tercih edin - Küçük diziler için stack ayırmayı kullanın
- String ayrıştırma işlemleri için
Span<T>
kullanmayı düşünün
-
Web API'lerde JSON Ayrıştırma
- Request/Response body'lerinin verimli işlenmesi
- HTTP başlıklarının ayrıştırılması
-
Dosya İşleme
- Büyük log dosyalarının ayrıştırılması
- CSV dosyalarının işlenmesi
-
Yüksek Performanslı Uygulamalar
- Gerçek zamanlı veri işleme
- Yüksek trafik senaryoları
- Mikroservis haberleşmeleri
[EN]
This project demonstrates the performance advantages of using Span<T>
and Memory<T>
in .NET Core applications. These types provide high-performance and low-allocation alternatives when working with arrays and strings.
The benchmark tests show comparative performance results between traditional approaches and Span-based approaches:
BenchmarkDotNet v0.14.0, Windows 11, Intel Core i7-1360P
.NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
| Operation | Mean Time | Memory Allocated |
|------------------------|---------------|------------------|
| Traditional String | 36.238 μs | 111,200 bytes |
| Span String | 24.023 μs | 79,920 bytes |
| Traditional Array Copy | 118.4 ns | 0 bytes |
| Span Array Copy | 110.4 ns | 0 bytes |
| Traditional Number Parse| 18.465 μs | 39,944 bytes |
| Span Number Parse | 10.022 μs | 0 bytes |
Key Observations:
-
String operations with Span:
- 33% faster execution
- 28% less memory allocation
-
Array copy operations:
- 7% performance improvement
- No additional memory allocation in both methods
-
Number parsing operations:
- 45% faster execution
- Zero additional memory allocation
-
SpanMemoryDemo.Core: Contains example implementations showing traditional and Span-based approaches
StringParsingExample.cs
: Examples of efficient string parsing without allocationsArrayOperationsExample.cs
: Memory-efficient array operations
-
SpanMemoryDemo.Benchmarks: Contains comparative performance tests of traditional and Span-based approaches
SpanBenchmarks.cs
: Comparative performance tests for various operations
-
String Parsing
- Traditional string splitting vs Span-based parsing
- Memory-efficient number parsing from strings
-
Array Operations
- Efficient array copying
- Stack-based array operations
- Array segment operations without allocations
-
Performance Tests
- Memory allocation comparisons
- Execution time comparisons
- Build the project in Release mode
- Navigate to SpanMemoryDemo.Benchmarks directory
- Run the command:
dotnet run -c Release
-
Reduced Memory Allocations
- Avoid string allocations during parsing
- Work with array segments without creating copies
-
Improved Performance
- Less garbage collection pressure
- More efficient memory usage
- Better cache utilization
-
Stack Allocation
- Ability to work with stack-allocated arrays
- Avoid heap allocations for temporary buffers
- Use
Span<T>
for stack-only operations - Use
Memory<T>
when you need to store the reference - Prefer
ReadOnlySpan<T>
when you don't need to modify the data - Use stack allocation for small arrays
- Consider using
Span<T>
for string parsing operations
-
JSON Parsing in Web APIs
- Efficient processing of request/response bodies
- Parsing HTTP headers
-
File Processing
- Parsing large log files
- Processing CSV files
-
High-Performance Applications
- Real-time data processing
- High-traffic scenarios
- Microservice communications