Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Last active December 30, 2015 19:29
Show Gist options
  • Select an option

  • Save masaru-b-cl/7874615 to your computer and use it in GitHub Desktop.

Select an option

Save masaru-b-cl/7874615 to your computer and use it in GitHub Desktop.
LINQ vs キーブレイク
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
class Sales
{
public string Customer { get; set; }
public string Product { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public override string ToString()
{
return string.Format("C:{0}, P:{1}, U:{2}, Q:{3}", Customer, Product, UnitPrice, Quantity);
}
}
static void Main(string[] args)
{
var q = from i in Enumerable.Range(1, 100)
from j in Enumerable.Range(1, 100)
from k in Enumerable.Range(1, 10)
select new Sales
{
Customer = string.Format("取引先{0:d2}", i),
Product = string.Format("商品{0:d2}", j),
UnitPrice = Convert.ToDecimal(j * 100),
Quantity = k
};
var source = q.ToArray();
const int times = 10;
var linqTime = UsingLinq(times, source);
var keyBreakTime = UsingKeyBreak(times, source);
Console.WriteLine();
Console.WriteLine("■LINQ:{0}", linqTime);
Console.WriteLine("■Key Break:{0}", keyBreakTime);
}
private static decimal UsingLinq(int times, IEnumerable<Sales> source)
{
var sw = new Stopwatch();
var totalTime = 0L;
for (var i = 0; i < times; i++)
{
sw.Reset();
sw.Start();
var salesOfCustomers = source.GroupBy(x => x.Customer);
foreach (var salesOfCustomer in salesOfCustomers)
{
var salesOfProducts = salesOfCustomer.GroupBy(x => (x.Product));
foreach (var salesOfProduct in salesOfProducts)
{
foreach (var sales in salesOfProduct)
{
Console.WriteLine(sales);
}
Console.WriteLine("小計 P:{0}, {1}, {2}", salesOfProduct.Key, salesOfProduct.Sum(x => x.Quantity),
salesOfProduct.Sum(x => x.UnitPrice * x.Quantity));
}
Console.WriteLine("大計 C:{0}, {1}, {2}", salesOfCustomer.Key, salesOfCustomer.Sum(x => x.Quantity),
salesOfCustomer.Sum(x => x.UnitPrice * x.Quantity));
}
Console.WriteLine("総計 {0}, {1}", source.Sum(x => x.Quantity), source.Sum(x => x.UnitPrice * x.Quantity));
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
totalTime += sw.ElapsedMilliseconds;
}
var averageTime = (decimal)totalTime / times;
return averageTime;
}
private static decimal UsingKeyBreak(int times, IEnumerable<Sales> source)
{
var sw = new Stopwatch();
var totalTime = 0L;
for (var i = 0; i < times; i++)
{
sw.Reset();
sw.Start();
var oldCustomer = "";
var newCustomer = "";
var oldProduct = "";
var newProduct = "";
var totalQuantity = 0L;
var totalPrice = 0m;
var totalCustomerQuantity = 0L;
var totalCustomerPrice = 0m;
var totalProductQuantity = 0L;
var totalProductPrice = 0m;
foreach (var sales in source)
{
if (oldCustomer == "")
{
oldCustomer = sales.Customer;
oldProduct = sales.Product;
}
newCustomer = sales.Customer;
newProduct = sales.Product;
if (oldCustomer != newCustomer || oldProduct != newProduct)
{
Console.WriteLine("小計 P:{0}, {1}, {2}", oldProduct, totalProductQuantity, totalProductPrice);
totalProductQuantity = 0L;
totalProductPrice = 0m;
oldProduct = newProduct;
}
if (oldCustomer != newCustomer)
{
Console.WriteLine("大計 P:{0}, {1}, {2}", oldCustomer, totalCustomerQuantity, totalCustomerPrice);
totalCustomerQuantity = 0L;
totalCustomerPrice = 0m;
oldCustomer = newCustomer;
}
totalProductQuantity += sales.Quantity;
totalProductPrice += sales.UnitPrice * sales.Quantity;
totalCustomerQuantity += sales.Quantity;
totalCustomerPrice += sales.UnitPrice * sales.Quantity;
totalQuantity += sales.Quantity;
totalPrice += sales.UnitPrice * sales.Quantity;
Console.WriteLine(sales);
}
Console.WriteLine("小計 P:{0}, {1}, {2}", oldProduct, totalProductQuantity, totalProductPrice);
Console.WriteLine("大計 P:{0}, {1}, {2}", oldCustomer, totalCustomerQuantity, totalCustomerPrice);
Console.WriteLine("総計 {0}, {1}", totalQuantity, totalPrice);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
totalTime += sw.ElapsedMilliseconds;
}
var averageTime = (decimal)totalTime / times;
return averageTime;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment