Jump to content

StringSplitToStruct


funkey
 Share

Recommended Posts

I am always looking for the fastest way to put a lot of data into DLLs. My new try is with a small DLL to split a data string and get a pointer to the new data array.

I hope this could be useful for someone else.

Please tell me if there is a faster way to do this!

Thanks!

#AutoIt3Wrapper_UseX64=n

; Set the data
Global $sData, $sDelim = ";"

For $i = 1 To 43200
$sData &= Random(0, 100) & $sDelim
Next
$sData &= Random(0, 100)

#region Float array to struct: Version 1 without DLL
Global $iStart = TimerInit()

Global $aData = StringSplit($sData, $sDelim, 2)
Global $tData = DllStructCreate("float[" & UBound($aData) & "]")
For $i = 0 To UBound($aData) - 1
DllStructSetData($tData, 1, $aData[$i], $i + 1)
Next

ConsoleWrite("Version 1 lasts: " & TimerDiff($iStart) & " ms" & @CRLF)

ConsoleWrite("First: " & DllStructGetData($tData, 1, 1) & @CRLF)
ConsoleWrite("Last : " & DllStructGetData($tData, 1, UBound($aData)) & @CRLF)

$tData = 0
#endregion Float array to struct: Version 1 without DLL

ConsoleWrite(@CRLF)

#region Float array to struct: Version 2 with DLL
Global $sDLL_StringSplit = "StringSplitToStruct.dll"
Global $hDLL_StringSplit = DllOpen($sDLL_StringSplit)

Global $iStart = TimerInit()

Global $iCount = 0
Global $tData2 = _StringSplitToStruct($sData, $sDelim, "float", $iCount)

ConsoleWrite("Version 2 lasts: " & TimerDiff($iStart) & " ms" & @CRLF)

ConsoleWrite("First: " & DllStructGetData($tData2, 1, 1) & @CRLF)
ConsoleWrite("Last : " & DllStructGetData($tData2, 1, $iCount) & @CRLF)

_StringSplitFree($tData2)

DllClose($hDLL_StringSplit)
#endregion Float array to struct: Version 2 with DLL



Func _StringSplitToStruct($sString, $sDelim, $sType, ByRef $iCount)
Local $aRet = DllCall($hDLL_StringSplit, "ptr:cdecl", "StringSplitToStruct", "str", $sString, "str", $sDelim, "str", $sType, "int*", $iCount)
$iCount = $aRet[4]
Local $tStruct = DllStructCreate($sType & "[" & $iCount & "]", $aRet[0])
Return $tStruct
EndFunc ;==>_StringSplitToStruct

Func _StringSplitFree(ByRef $tStruct)
DllCall("msvcrt.dll", "none:cdecl", "free", "ptr", DllStructGetPtr($tStruct))
$tStruct = 0
EndFunc ;==>_StringSplitFree

PVOID DLL_EXPORT StringSplitToStruct(LPSTR datastring, LPCSTR delim, LPCSTR type, int* iCount)
{
    char* p;
    int i;

    if(*iCount == 0)
    {
        *iCount = 0;
        for(i = 0; datastring[i]; i++)
        {
            if(datastring[i] == delim[0])
            {
                (*iCount)++;
            }
        }

        (*iCount)++;
    }

    i = 0;
    p = strtok(datastring, delim);

    if(stricmp(type, "float") == 0)
    {
        float* buffer = (float*)malloc(*iCount * sizeof(float));
        while ((p != NULL) && (i < *iCount))
        {
            buffer[i] = atof(p);
            p = strtok (NULL, delim);
            i++;
        }
        return buffer;
    }
    else if(stricmp(type, "int") == 0)
    {
        int* buffer = (int*)malloc(*iCount * sizeof(int));
        while ((p != NULL) && (i < *iCount))
        {
            buffer[i] = atoi(p);
            p = strtok (NULL, delim);
            i++;
        }
        return buffer;
    }

    return NULL;
}

StringSplitToStruct.rar

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Thanks,

but this is C, not C++ and the code should be easy to understand. ;)

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...