26.08.2014 Views

C# e Orientação a Objetos

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

COLLECTIONS 152<br />

Exercícios de Fixação<br />

7 Vamos comparar o tempo do método Contains() das listas e dos conjuntos.<br />

1 using System ;<br />

2 using System . Collections . Generic ;<br />

3 using System . Diagnostics ;<br />

4<br />

5 public class TestaContains<br />

6 {<br />

7 static void Main ()<br />

8 {<br />

9 List arrayList = new List () ;<br />

10 HashSet hashSet = new HashSet () ;<br />

11<br />

12 long tempo = TestaContains . Contains ( arrayList );<br />

13 Console . WriteLine (" ArrayList : " + tempo + "ms");<br />

14<br />

15 tempo = TestaContains . Contains ( hashSet );<br />

16 Console . WriteLine (" HashSet : " + tempo + "ms");<br />

17<br />

18 }<br />

19<br />

20 public static long Contains ( ICollection colecao )<br />

21 {<br />

22 int size = 100000;<br />

23<br />

24 for ( int i = 0; i < size ; i ++)<br />

25 {<br />

26 colecao . Add (i);<br />

27 }<br />

28<br />

29 Stopwatch sw = new Stopwatch ();<br />

30 sw. Start ();<br />

31<br />

32 for ( int i = 0; i < size ; i ++)<br />

33 {<br />

34 colecao . Contains (i);<br />

35 }<br />

36<br />

37 sw. Stop ();<br />

38<br />

39 return sw. ElapsedMilliseconds ;<br />

40 }<br />

41 }<br />

Código <strong>C#</strong> 17.23: TestaContains.cs<br />

152 www.k19.com.br

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!