c# - Enumerating Datatable rows in batches efficiently -
i'd enumerate through datatable in batches. so, i've created method returns ienumerable<datatable> method looks this:
public ienumerable<datatable> enumeraterowsinbatches( datatable table, int batchsize ) { int rowcount = table.rows.count; int batchindex = 0; while( batchindex * batchsize < rowcount ) { datatable result = table.clone(); int batchstart = batchindex * batchsize; int batchlimit = ( batchindex + 1 ) * batchsize; if( rowcount < batchlimit ) batchlimit = rowcount; for( int = batchstart; < batchlimit; i++ ) { result.importrow( table.rows[ ] ); } batchindex++; yield return result; } } this works pretty nicely actually. i'm iterating through these batches in order send sql server using table valued parameter. i'm seeing importrow taking majority of elapsed time , i'd speed up.
i'm looking how that. i'm free treat data read-only , sense copying rows not strictly necessary here.
i came approach lead ~40% performance improvement in tests:
public static ienumerable<datatable> enumeraterowsinbatches(datatable table, int batchsize) { int rowcount = table.rows.count; int batchindex = 0; datatable result = table.clone(); // not change, avoid recreate while (batchindex * batchsize < rowcount) { result.rows.clear(); // reuse datatable, clear previous results int batchstart = batchindex * batchsize; int batchlimit = (batchindex + 1) * batchsize; if (rowcount < batchlimit) batchlimit = rowcount; (int = batchstart; < batchlimit; i++) result.rows.add(table.rows[i].itemarray); // avoid importrow batchindex++; yield return result; } }
Comments
Post a Comment