Linq为什么慢? [英] Why is Linq slower?

查看:88
本文介绍了Linq为什么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


测试som Linq-expressions并尝试测量性能和

将它与Linq之前的编程进行比较。


以下两种方法功能相同,但非Linq方法的价格是

的两倍。


public List< ConferenceRoomOldWay(int minimumSeatingCapacity)

{

列表< ConferenceRoomavailableRooms =新列表< ConferenceRoom>();


foreach(会议室房间)

{

if(room.SeatingCapacity> = minimumSeatingCapacity)

availableRooms.Add(room);

}


返回可用房间;

}


公共列表< ConferenceRoomNewWay(int minimumSeatingCapacity)

{

返回(从房间的房间

,其中room.SeatingCapacity> = minimumSeatingCapacity

选择房间).ToList();

}


PS。

简单明了:


达teTime start = DateTime.Now;

for(int i = 0;我< 1000000; i ++)

rooms.OldWay(8);


Console.WriteLine((DateTime.Now - start).Ticks);


....来衡量表现(一遍又一遍地重复测试)。


结果:

非Linq:约6 300 000蜱

Linq:ca。 3 100 000


为什么Linq更慢?


谢谢

/ Paul

解决方案

结果:


Non-Linq:ca。 6 300 000蜱

Linq:ca。 3 100 000



我在这里缺少什么?更少的嘀嗒声实际上意味着它更快,对吧?


-Jeroen


抱歉结果如下:


结果:

Linq:ca。 6 300 000蜱

非Linq:ca。 3 100 000


嗨!


测试som Linq表情并尝试测量性能和

将它与Linq之前的编程进行比较。


以下两种方法功能相同,但非Linq方法的速度是b / b
的两倍。


public List< ConferenceRoomOldWay(int minimumSeatingCapacity)

{

List< ConferenceRoomavailableRooms = new List< ConferenceRoom>();


foreach(会议室房间)

{

if(room.SeatingCapacity> = minimumSeatingCapacity)

availableRooms.Add(room);

}


返回可用房间;

}


public List< ConferenceRoomNewWay(int minimumSeatingCapacity)

{

返回(从房间的房间

,其中room.SeatingCapacity> = minimumSeatingCapacity

sel等房间).ToList();

}


PS。

只是简单明了:


DateTime start = DateTime.Now;

for(int i = 0;我< 1000000; i ++)

rooms.OldWay(8);


Console.WriteLine((DateTime.Now - start).Ticks);


....来衡量表现(一遍又一遍地重复测试)。


结果:

非Linq:约6 300 000蜱

Linq:ca。 3 100 000


为什么Linq更慢?


谢谢

/ Paul


对不起,刚刚发布了一条回复说我混合了结果。


它应该是副verca。


谢谢


" Jeroen"写道:


结果:

非Linq:ca。 6 300 000蜱

Linq:ca。 3 100 000



我在这里缺少什么?更少的蜱真的意味着它更快,对吧?


-Jeroen


Hi!

Testing som Linq-expressions and tried to measure performance and
compare it to pre-Linq programming.

The folloing two methods are functional equal but the non-Linq one is
twice as fast.

public List<ConferenceRoomOldWay(int minimumSeatingCapacity)
{
List<ConferenceRoomavailableRooms = new List<ConferenceRoom>();

foreach (ConferenceRoom room in rooms)
{
if (room.SeatingCapacity >= minimumSeatingCapacity)
availableRooms.Add(room);
}

return availableRooms;
}

public List<ConferenceRoomNewWay(int minimumSeatingCapacity)
{
return (from room in rooms
where room.SeatingCapacity >= minimumSeatingCapacity
select room).ToList();
}

PS.
Just did a plain and simple:

DateTime start = DateTime.Now;
for (int i = 0; i < 1000000; i++)
rooms.OldWay(8);

Console.WriteLine((DateTime.Now - start).Ticks);

....to mesaure the performance (Did repeate the test over and over).

Results:
Non-Linq: ca. 6 300 000 ticks
Linq: ca. 3 100 000

Why is Linq slower?

Thanks
/Paul

解决方案

Results:

Non-Linq: ca. 6 300 000 ticks
Linq: ca. 3 100 000

What am I missing here? Less ticks actually means it''s faster, right?

-Jeroen


Sorry mixed the results:

Results:
Linq: ca. 6 300 000 ticks
Non-Linq: ca. 3 100 000

"pa**********@hotmail.com" wrote:

Hi!

Testing som Linq-expressions and tried to measure performance and
compare it to pre-Linq programming.

The folloing two methods are functional equal but the non-Linq one is
twice as fast.

public List<ConferenceRoomOldWay(int minimumSeatingCapacity)
{
List<ConferenceRoomavailableRooms = new List<ConferenceRoom>();

foreach (ConferenceRoom room in rooms)
{
if (room.SeatingCapacity >= minimumSeatingCapacity)
availableRooms.Add(room);
}

return availableRooms;
}

public List<ConferenceRoomNewWay(int minimumSeatingCapacity)
{
return (from room in rooms
where room.SeatingCapacity >= minimumSeatingCapacity
select room).ToList();
}

PS.
Just did a plain and simple:

DateTime start = DateTime.Now;
for (int i = 0; i < 1000000; i++)
rooms.OldWay(8);

Console.WriteLine((DateTime.Now - start).Ticks);

....to mesaure the performance (Did repeate the test over and over).

Results:
Non-Linq: ca. 6 300 000 ticks
Linq: ca. 3 100 000

Why is Linq slower?

Thanks
/Paul


Sorry, just posted a reply saying that I mixed the results.

Its supposed to be vice-verca.

thanks

"Jeroen" wrote:

Results:
Non-Linq: ca. 6 300 000 ticks
Linq: ca. 3 100 000


What am I missing here? Less ticks actually means it''s faster, right?

-Jeroen


这篇关于Linq为什么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆