Click here to Skip to main content
Click here to Skip to main content

Fox vs. Rabbit or Structure vs. Class

, 1 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Books teach the advantages of structures over classes. I wanted to know it better!

Introduction

Is it not quiet some indoctrination referring to classes at all times? Books teach the advantages of structures over classes. I wanted to know it better! What run time advantages really occur? When is it for performance reason better to use which one? How about coding difference? It might be said that a structure outperforms a class by far. But how much is far exactly?

In the example a packed structure and a packed class with the same fields was used, accessed 1,000,000 times and the runtime measured.

Code

#region Structures
public struct lexerWords
{
    public Color co;
    public bool bold;
    public string name;
    public bool italic;
    public bool underline;
    public bool caseSensitive;
    public bool isSet;
    public int closeOn;
    public string words;
    
    public lexerWords(Color Color, string Name, bool Bold, bool Italic, bool Underline, 
           bool CaseSensitive, bool IsSet, int CloseOn, String Words)
    {
        co = Color; name = Name; words = Words;
        bold = Bold; italic = Italic; underline = Underline;
        caseSensitive = CaseSensitive; isSet = IsSet; closeOn = CloseOn;
    }
}

public struct lexerInfo
{
    public string name;
    public string listName;
    public int indent;
    public string punctChars;
    public string lineComment;
    public string streamStart;
    public string streamEnd;
    public Color acColor;
    public string autocompleteWords;
    public lexerWords[] wordLists;
    
    public lexerInfo(string Name, string ListName, int Indent, string PunctChars, string LineComment, 
           string StreamStart, string StreamEnd, Color AcColor, string AutoCompleteWords,
    params lexerWords[] WordLists)
    {
        name = Name; listName = ListName; indent = Indent; punctChars = PunctChars; 
        lineComment = LineComment;
        streamStart = StreamStart; streamEnd = StreamEnd;
        acColor = AcColor; autocompleteWords = AutoCompleteWords;
        wordLists = WordLists;
    }
}
#endregion Structures

#region classes
public class lexerInfoClass
{
    public string _name;
    public string listName;
    public int indent;
    public string punctChars;
    public string lineComment;
    public string streamStart;
    public string streamEnd;
    public Color acColor;
    public string autocompleteWords;
    public lexerWordsClass[] wordLists;

    public lexerInfoClass(string Name, string ListName, int Indent, string PunctChars, 
           string LineComment, string StreamStart, string StreamEnd,
           Color AcColor, string AutoCompleteWords,
    params lexerWordsClass[] WordLists)
    {
        _name = Name; listName = ListName; indent = Indent; punctChars = PunctChars; 
        lineComment = LineComment;
        streamStart = StreamStart; streamEnd = StreamEnd;
        acColor = AcColor; autocompleteWords = AutoCompleteWords;
        wordLists = WordLists;
    }

    public bool this[string Name, string ListName, int Indent, string PunctChars, 
           string LineComment, string StreamStart, string StreamEnd,
           Color AcColor, string AutoCompleteWords, params lexerWordsClass[] WordLists]
    {
        get
        {
            Name = _name; ListName = listName; Indent = indent; 
            PunctChars = punctChars; LineComment = lineComment;
            StreamStart = streamStart; StreamEnd = streamEnd;
            AcColor = acColor; AutoCompleteWords = autocompleteWords;
            WordLists = wordLists;
            return true;
        }
        set
        {
            _name = Name; listName = ListName; indent = Indent; punctChars = PunctChars; 
            lineComment = LineComment;
            streamStart = StreamStart; streamEnd = StreamEnd;
            acColor = AcColor; autocompleteWords = AutoCompleteWords;
            wordLists = WordLists; 
        }
    }

    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
    public string ListName
    {
        get { return listName; }
        set { listName = value; }
    }
    public int Indent
    {
        get { return indent; }
        set { indent = value; }
    }
    public string PunctChars
    {
        get { return punctChars; }
        set { punctChars = value; }
    }
    public string LineComment
    {
        get { return lineComment; }
        set { lineComment = value; }
    }
    public string StreamStart
    {
        get { return streamStart; }
        set { streamStart = value; }
    }
    public string StreamEnd
    {
        get { return streamEnd; }
        set { streamEnd = value; }
    }
    public Color AcColor
    {
        get { return acColor; }
        set { acColor = value; }
    }
    public string AutoCompleteWords
    {
        get { return autocompleteWords; }
        set { autocompleteWords = value; }
    }
    public lexerWordsClass[] WordLists
    {
        get { return wordLists; }
        set { wordLists = value; }
    }
}
#endregion classes

Time for 1Mio cycles (read and write) in seconds, sequence Structure before Class:

Structure:	0,086
Class:		0,032

Altering the sequence to Class before Structure reveals times like:

Class:		0,039
Structure:	0,059

Windows busy everywhere behavior and passing on data by value or reference was neglected in the test since it does not make sense at these speeds and alternatives to pay close attention to such factors over all.

So, perhaps it is not all that critical if an application has to wait frequently for user input but it can make just the difference for large scale database access or time critical applications. Perhaps nowadays the additional coding for classes as well as the familiarity might rather be an indication for the final decision. Would it not be for curiosity reasons, the example might prove useless considering all factors.

Warranty

There is absolutely no warranty whatsoever implied or assumed. Use it at your own risk. It does a marvelous job for the author. Copyrights and Trademarks shall belong to their respective owners. I am not going to fight over that!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

cyprussun

Philippines Philippines
Grew up in a metal processing company and did industrial HW/SW development since the birth of Intel’s 8080. Lectured IT since 1986 at several levels. Hobbies, sidesteps: Woodworking and deep sea diving. Background: ASM, C, C++. Platforms: Win, Novel, CP/M, MP/M, DOS, (Linux).
 
It’s not the developer’s duty to pay up for the engineer’s ignorance.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 1 Mar 2013
Article Copyright 2013 by cyprussun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid