Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save AlbertoMonteiro/bcd834d097097b363f2d to your computer and use it in GitHub Desktop.

Select an option

Save AlbertoMonteiro/bcd834d097097b363f2d to your computer and use it in GitHub Desktop.
Resposta Sergio
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
namespace Sergio
{
public class EnumerableAlberto
{
public static void Executa()
{
var stopwatch = Stopwatch.StartNew();
var pessoas = ObterPessoas();
//var pessoas = ObterPessoasMachine();
stopwatch.Stop();
Console.WriteLine("ObterPessoas Elapsed: {0}", stopwatch.Elapsed);
stopwatch = Stopwatch.StartNew();
var pessoa = pessoas.First(p2 => p2.ID == 10);
stopwatch.Stop();
Console.WriteLine("pessoas.Where Elapsed: {0}", stopwatch.Elapsed);
stopwatch = Stopwatch.StartNew();
Console.WriteLine("Id: {0} - Nome: {1}", pessoa.ID, pessoa.Nome);
stopwatch.Stop();
Console.WriteLine("Exibe pessoa Elapsed: {0}", stopwatch.Elapsed);
}
private static IEnumerable<Pessoa> ObterPessoasMachine()
{
return new ObtemPessoasStateMachine(-2);
}
private static IEnumerable<Pessoa> ObterPessoas()
{
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder { IntegratedSecurity = true, InitialCatalog = "SergioDb", DataSource = @"(localdb)\v11.0" };
const string SQL = "SELECT * FROM Pessoa";
using (var conn = new SqlConnection(sqlConnectionStringBuilder.ConnectionString))
{
conn.Open();
var com = new SqlCommand(SQL, conn);
var sqlDataReader = com.ExecuteReader();
while (sqlDataReader.Read())
yield return new Pessoa { ID = (long)sqlDataReader["ID"], Nome = (string)sqlDataReader["Nome"] };
sqlDataReader.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
namespace Sergio
{
public class EnumerableSergio
{
public static void Executa()
{
var stopwatch = Stopwatch.StartNew();
var pessoas = ObterPessoas();
stopwatch.Stop();
Console.WriteLine("ObterPessoas Elapsed: {0}", stopwatch.Elapsed);
stopwatch = Stopwatch.StartNew();
var pessoa = pessoas.First(o => o.ID < 10);
stopwatch.Stop();
Console.WriteLine("pessoas.Where Elapsed: {0}", stopwatch.Elapsed);
stopwatch = Stopwatch.StartNew();
Console.WriteLine("Id: {0} - Nome: {1}", pessoa.ID, pessoa.Nome);
stopwatch.Stop();
Console.WriteLine("Exibe pessoa Elapsed: {0}", stopwatch.Elapsed);
}
private static IEnumerable<Pessoa> ObterPessoas()
{
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder { IntegratedSecurity = true, InitialCatalog = "SergioDb", DataSource = @"(localdb)\v11.0" };
var ds = new DataSet();
var conn = new SqlConnection(sqlConnectionStringBuilder.ConnectionString);
conn.Open();
var sql = "SELECT * FROM Pessoa";
var da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
foreach (DataRow item in ds.Tables[0].Rows)
yield return new Pessoa { ID = (long)item["ID"], Nome = (string)item["Nome"] };
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;
namespace Sergio
{
internal class ObtemPessoasStateMachine : IEnumerable<Pessoa>, IEnumerable, IEnumerator<Pessoa>, IEnumerator, IDisposable
{
private readonly int initialThreadId;
public SqlDataReader SqlDataReader;
public SqlConnection SqlConnection;
private Pessoa pessoa;
private int state;
public ObtemPessoasStateMachine(int state)
{
this.state = state;
initialThreadId = Thread.CurrentThread.ManagedThreadId;
}
IEnumerator<Pessoa> IEnumerable<Pessoa>.GetEnumerator()
{
ObtemPessoasStateMachine result;
if (Thread.CurrentThread.ManagedThreadId == initialThreadId && state == -2)
{
state = 0;
result = this;
}
else
result = new ObtemPessoasStateMachine(0);
return result;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<Pessoa>)this).GetEnumerator();
}
Pessoa IEnumerator<Pessoa>.Current { get { return pessoa; } }
object IEnumerator.Current { get { return pessoa; } }
bool IEnumerator.MoveNext()
{
bool result;
try
{
switch (state)
{
case 0:
state = -1;
var sqlConnectionStringBuilder2 = new SqlConnectionStringBuilder { IntegratedSecurity = true, InitialCatalog = "SergioDb", DataSource = "(localdb)\\v11.0" };
SqlConnection = new SqlConnection(sqlConnectionStringBuilder2.ConnectionString);
state = 1;
SqlConnection.Open();
using (var sqlCommand = new SqlCommand("SELECT * FROM Pessoa", SqlConnection))
SqlDataReader = sqlCommand.ExecuteReader();
goto dataReaderRead;
case 2:
state = 1;
goto dataReaderRead;
}
goto IL_16D;
dataReaderRead:
if (SqlDataReader.Read())
{
pessoa = new Pessoa { ID = (long)SqlDataReader["ID"], Nome = (string)SqlDataReader["Nome"] };
state = 2;
return true;
}
SqlDataReader.Close();
FinishHim();
IL_16D:
result = false;
}
catch
{
((IDisposable)this).Dispose();
throw;
}
return result;
}
void IEnumerator.Reset()
{
throw new NotSupportedException();
}
void IDisposable.Dispose()
{
switch (state)
{
case 1:
break;
case 2:
break;
default:
return;
}
try { }
finally
{
FinishHim();
}
}
private void FinishHim()
{
state = -1;
if (SqlConnection != null)
SqlConnection.Dispose();
}
}
}
namespace Sergio
{
internal class Pessoa
{
public long ID { get; set; }
public string Nome { get; set; }
}
}
using System;
using System.Diagnostics;
namespace Sergio
{
internal class Program
{
private static void Main(string[] args)
{
var stopwatch = Stopwatch.StartNew();
EnumerableSergio.Executa();
stopwatch.Stop();
Console.WriteLine("Sergio total: {0}", stopwatch.Elapsed);
Console.WriteLine("\n\n========================================\n\n");
stopwatch.Restart();
EnumerableAlberto.Executa();
stopwatch.Stop();
Console.WriteLine("Alberto total: {0}", stopwatch.Elapsed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment