HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

X12 Reader (C#) Performance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
performancex12reader

Problem

Is there any way that I can improve the performance of my x12_reader? The main functions that seem to be the bottlenecks are read_line() and get_element() they are both called upwards of millions of times.

```
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace X12ReprocessApp {

public class segment {
public String line_text = "";
public String loop_id = "";
public String id = null;

///
///Returns a string containing the element from the current segment.
///
///The element number that you want to return
public String get_element(int element_number) {
int count = 0;
int start_index = 0;
int end_index = 0;
int current_index = 0;

while (count
///Returns a decimal containing the element from the current segment.
///
///The element number that you want to return
public Decimal get_element_as_number(int element_number) {
Decimal i = (Decimal.TryParse(get_element(element_number), out i)) ? i : 0;
return i;
}

///
///Return added total of the elements within the segment.
///
///Element number to start adding.
///Increment of element position after each add.
public Decimal get_elements_total(int start, int step) {
Decimal total = 0;
for (int i = start; i = 0) {
id = line_text.Substring(0, index);
return id;
}
return String.Empty;
}

public int get_length() {
int count = 1;
for (int i = 0; i
///Returns a segment with the selected element replaced with the string value passed.
///
///The element that you want to replace.
///The string value with which you want to replace the element.
public segment replace_element(int element_number, String value) {
String[] elements = line_text.Split(x12_reader.element_delimiter);
if (element_number 0) {

Solution

Some quick remarks WRT your coding style, which doesn't follow Microsoft's guidelines:

  • segment: class names should be PascalCase.



  • get_element: method names should be PascalCase.



  • start_index: don't use non-alphanumeric characters in method names, class names, etc.



  • String, Decimal, Int32,...: usually the aliases are preferred (string, decimal, int,...).



  • get_element_as_number(int element_number): parameters should be camelCase.



  • public segment[] segments;: properties should be PascalCase and should have a getter and/or a setter.

Context

StackExchange Code Review Q#140116, answer score: 3

Revisions (0)

No revisions yet.