Optimizing List<string>

Optimizing List<string>

Content Index :

Optimizing List<string>
Tag : chash , By : kiirpi
Date : November 29 2020, 09:01 AM

this one helps. the method ReadlAllLines returns an array of string, not a List => File.ReadAllLines Method (String)
I think that you shuld use ReadLines(), check this Question about the diferences between ReadLines and ReadlAllLines:
string[] lines = File.ReadAllLines("C:\\mytxt.txt");
foreach (var line in File.ReadLines("C:\\mytxt.txt"))

   //Do something     


No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Optimizing java heap usage by String using StringBuffer , StringBuilder , String.intern()

Tag : java , By : Ken
Date : March 29 2020, 07:55 AM
Does that help What I do is is have one or more String pools. I do this to a) not create new Strings if I have one in the pool and b) reduce the retained memory size, sometimes by a factor of 3-5. You can write a simple string interner yourself but I suggest you consider how the data is read in first to determine the optimal solution. This matters as you can easily make matters worse if you don't have an efficient solution.
As EJP points out processing a line at a time is more efficient, as is parsing each line as you read it. i.e. an int or double takes up far less space than the same String (unless you have a very high rate of duplication)
public class StringInterner {
    private final String[] interner;
    private final int mask;

    public StringInterner(int capacity) {
        int n = nextPower2(capacity, 128);
        interner = new String[n];
        mask = n - 1;

    public String intern(@NotNull CharSequence cs) {
        long hash = 0;
        for (int i = 0; i < cs.length(); i++)
            hash = 57 * hash + cs.charAt(i);
        int h = hash(hash) & mask;
        String s = interner[h];
        if (isEqual(s, cs))
            return s;
        String s2 = cs.toString();
        return interner[h] = s2;

    static boolean isEqual(@Nullable CharSequence s, @NotNull CharSequence cs) {
        if (s == null) return false;
        if (s.length() != cs.length()) return false;
        for (int i = 0; i < cs.length(); i++)
            if (s.charAt(i) != cs.charAt(i))
                return false;
        return true;

    static int nextPower2(int n, int min) {
        if (n < min) return min;
        if ((n & (n - 1)) == 0) return n;
        int i = min;
        while (i < n) {
            i *= 2;
            if (i <= 0) return 1 << 30;
        return i;

    static int hash(long n) {
        n ^= (n >> 43) ^ (n >> 21);
        n ^= (n >> 15) ^ (n >> 7);
        return (int) n;

Python, optimizing a list comprehension for string concatenation

Tag : python , By : gorbiz
Date : March 29 2020, 07:55 AM
Does that help I'm using cProfile to run benchmarks on a script that process strings via list comprehension. The target line I'd like to optimize looks like this: , This should bring at least a minimum improvement:
# First precalculate the static part of the string
template = 'prefix%s_' % day + '%s'

# Then, use the %s string interpolation instead of joining strings with '+'  
# -->> Note: this proved to be wrong later... 
signals = [template % s for s in signals]

# Alternatively you could use map to compare efficiency (be aware of differences between python 2 and 3)
signals = map(template.__mod__, signals)
>>> import timeit
>>> day = 45
>>> signals = ['aaaa', 'bbbb', 'cccccc', 'dddddddddddd']
>>> timeit.timeit("[('prefix' + str(day) + '_' + s) for s in signals]", 'from __main__ import day, signals')
>>> template = 'prefix%s_' % day + '%s'
>>> timeit.timeit("[template % s for s in signals]", 'from __main__ import template, signals')
>>> timeit.timeit("map(template.__mod__, signals)", 'from __main__ import template, signals')
>>> template = 'prefix%s_' % day
>>> timeit.timeit("[template + s for s in signals]", 'from __main__ import template, signals')

Optimizing Duplicate node search(Closed list,Open List) on N-Puzzle using A-star

Tag : algorithm , By : Alex Sadzawka
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Instead of searching through all the nodes sequentially, you may want to keep a map or set of nodes using the grid for comparison:
struct GridLess {
    bool operator()(const Node *a,const Node *b) const
        for(int i=0;i<N;i++)
           for(int j=0;j<N;j++)
                   return a->Grid[i][j] < b->Grid[i][j];
        return false;

std::set<Node*,GridLess> closed_list;
if (closed_list.count(temp_Node)==0) {
    // No node in closed_list has the same grid as temp_node

Optimizing my insertion to SQL table while parsing through a long list of string

Tag : chash , By : RinKaMan
Date : March 29 2020, 07:55 AM
Any of those help I think what you're looking for is the SqlBulkCopy. I had to use it a little bit ago to populate a bunch of data into SQL for analysis purposes - here's what my function looked like:
private void PopulateTable(DataTable data, string tableName)

    string connectionString = @"Server=PC40808\SQLEXPRESS;Database=Scratchpad;Trusted_Connection=True;";

    SqlConnection conn = new SqlConnection(connectionString);
    SqlTransaction transaction = conn.BeginTransaction();
        SqlBulkCopy copy = new SqlBulkCopy(conn, SqlBulkCopyOptions.KeepIdentity, transaction);
        copy.DestinationTableName = tableName;

    catch (Exception ex)

private DataTable GetBlankDTForRetrievals()
    DataTable retVal = new DataTable("retrievals");
    retVal.Columns.Add("ObjectID", typeof(string));
    retVal.Columns.Add("DateTimeStamp", typeof(string));
    retVal.Columns.Add("Username", typeof(string));
    retVal.Columns.Add("DocClass", typeof(string));
    retVal.Columns.Add("Func", typeof(string));
    return retVal;
private DataTable GetDataTableForSingleRetrievalFile(string fileLoc)
    DataTable retVal = GetBlankDTForRetrievals();
    string[] lines = System.IO.File.ReadAllLines(fileLoc);
    foreach(string line in lines.Where(l => l.Length > 94).Where((l) => l.StartsWith(" ")))
        DataRow rowToAdd = retVal.NewRow();
        rowToAdd["ObjectID"] = line.Substring(48, 15);
        rowToAdd["DateTimeStamp"] = line.Substring(1, 17);
        rowToAdd["Username"] = line.Substring(32, 15);
        rowToAdd["DocClass"] = line.Substring(69, 20);
        rowToAdd["Func"] = line.Substring(90, 4);
    return retVal;

Optimizing big string arrays for checking if a string exists queries

Tag : php , By : Topher Cyll
Date : March 29 2020, 07:55 AM
around this issue
I would suggest you to do sorting with binary search to know if a value exists. Time Complexity will be O(N log N) for sorting and O(log N) to search each individual element, where N is the number of elements in the array.

function checkIfValueExists($arr,$search_value){

    $low  = 0;
    $high = count($arr) - 1;

    while($low <= $high){
        $mid = $low + intval(($high - $low) / 2);
        $compare_result = strcmp($arr[$mid],$search_value);
        if($compare_result === 0) return true;
        else if($compare_result < 0) $low = $mid + 1;
        else $high = $mid - 1;

    return false;

$arr = array();

$str = "abcdefghijklmnopqrstuvwxyz";

$values_to_check = array();

    $str_length = rand(1,50);
    $new_str = "";
    while($str_length-- > 0){
        $new_str .= $str[rand(0,25)];

    $arr[]  = $new_str;
    if(rand(0,1) === 1){
        $values_to_check[] = rand(0,1) === 1 ? $new_str . $str[rand(0,25)] : $new_str;

// sort the array of strings.


// test the functionality

foreach($values_to_check as $each_value){
    echo "<br/>";
Related Posts Related QUESTIONS :
  • Queryfilter on ApplicationUser in OnModelCreating in ApplicationDbContext creates StackOverflowException
  • How to get record form a different table based on a value from first table with linq expression?
  • Show data in Grid from returned model
  • Using Attributes to Override Data Model Conventions
  • Basic OOP console calculator, result Error
  • Compositon and Repository pattern
  • Multiple using statements with if condition
  • How do i increase a number by 1 in every line that contain the number 1
  • Add binding to elements that are created in codebehind
  • How to add a column in an existing AspNetUsers table
  • Order a list of elements with another list of doubles
  • How to setup a NuGet package to copy content files to output build directory?
  • In SignalR Core using ChannelWriter: Do I need to call TryComplete twice if there's an exception?
  • C# GetProcessesByName: issue with colon
  • c# wpf | create complex object with user-defined name to Serialize into JSON
  • How can I get a instance of a generic list with reflection?
  • WPF XAML - Design time and visibility of textbox
  • EF Core and MySql query is too slow
  • Getting Registered App Display Name from an App Id
  • How to get all variables from a string
  • Delete entity with all childs connected
  • Azure Build agent cant´t find class library referance
  • Initialize Nested Dictionaries in c#
  • .Net Core Binding
  • Generic event test method, preventing code duplication
  • How do I keep the ellipses in the center when the screen is resized
  • How to require a property using JsonSchema.NET?
  • C# XDocument Element/Elements returns null
  • Autofac keyed service with IEnumerable relationship type
  • Installing EntityFramework via NuGet manager
  • Always Check if there is Internet Connection Xamarin forms
  • WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false
  • Can we use JsonRequestBehavior.AllowGet with [HttpPost] attribute?
  • How to customize the Setup wizard with custom forms in Visual Studio setup project
  • C# ASP.NET - Use method from another class to create labels
  • C# List IList or IEnumerable as argument
  • Parsing File with C# And Replace method
  • Losing special unicode characters in encryption (C#)
  • Getting stored procedure returned value instead of row affected
  • How can I construct HTML using NameValuePair in android?
  • Loading a pop up page in ASP.net through a js file
  • How to pass alert or notification message from controller to View?
  • C# to pause, turn on ssas server, backup cube.... how to?
  • How to execute DataTable.Select() for a column of custom class type for a particular element in that C#
  • how to connect mysql8.0 with C#
  • Passing incorrect values into MultiValueConverter by MultiBinding
  • Can i use IEnumerator as Update func?
  • How to convert API Json response to C# Array?
  • Blazor Textfield Oninput User Typing Delay
  • Performing both layout and render transform results in wrong output
  • uwp beforetextchanged cursor moving in front of text
  • How to keep duplicates from a string[] exclude words from a List and print them out
  • .Net Core Strings.Asc/Mid/Chr/Len missing even after importing Microsoft.VisualBasic
  • How to return to previous search page without being asked to Confirm Form Re-submission and keeping the results on ASP.N
  • How set a identity scaffolding item/page how initial page in asp.net MVC core?
  • LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
  • What is "ByteArray.uncompress()" in AS3 equivalent to in C#?
  • Getting a specific letter from a string variable for my simple guessing game for clues
  • Send an email with Outlook without a subject --- dialog box issue
  • passing List<MyModel> from my controller in the "WebInterfaceProject" to the processor method in "D
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com