Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 185899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:33:09+00:00 2026-05-11T15:33:09+00:00

I am writing a piece of code designed to do some data compression on

  • 0

I am writing a piece of code designed to do some data compression on CLSID structures. I’m storing them as a compressed stream of 128 bit integers. However, the code in question has to be able to place invalid CLSIDs into the stream. In order to do this, I have left them as one big string. On disk, it would look something like this:

+--------------------------+-----------------+------------------------+ |                          |                 |                        | | Length of Invalid String | Invalid String  | Compressed Data Stream | |                          |                 |                        | +--------------------------+-----------------+------------------------+ 

To encode the length of the string, I need to output the 32 bit integer that is the length of the string one byte at a time. Here’s my current code:

std::vector<BYTE> compressedBytes; DWORD invalidLength = (DWORD) invalidClsids.length(); compressedBytes.push_back((BYTE)  invalidLength        & 0x000000FF); compressedBytes.push_back((BYTE) (invalidLength >>= 8) & 0x000000FF)); compressedBytes.push_back((BYTE) (invalidLength >>= 8) & 0x000000FF)); compressedBytes.push_back((BYTE) (invalidLength >>= 8)); 

This code won’t be called often, but there will need to be a similar structure in the decoding stage called many thousands of times. I’m curious if this is the most efficient method or if someone can come up with one better?

Thanks all!

Billy3

EDIT: After looking over some of the answers, I created this mini test program to see which was the fastest:

// temp.cpp : Defines the entry point for the console application. //  #include 'stdafx.h' #include <windows.h> #include <ctime> #include <iostream> #include <vector>  void testAssignedShifts(); void testRawShifts(); void testUnion();  int _tmain(int argc, _TCHAR* argv[]) {     std::clock_t startTime = std::clock();     for (register unsigned __int32 forLoopTest = 0; forLoopTest < 0x008FFFFF; forLoopTest++)     {         testAssignedShifts();     }     std::clock_t assignedShiftsFinishedTime = std::clock();     for (register unsigned __int32 forLoopTest = 0; forLoopTest < 0x008FFFFF; forLoopTest++)     {         testRawShifts();     }     std::clock_t rawShiftsFinishedTime = std::clock();     for (register unsigned __int32 forLoopTest = 0; forLoopTest < 0x008FFFFF; forLoopTest++)     {         testUnion();     }     std::clock_t unionFinishedTime = std::clock();     std::printf(         'Execution time for assigned shifts: %08u clocks\n'         'Execution time for raw shifts:      %08u clocks\n'         'Execution time for union:           %08u clocks\n\n',         assignedShiftsFinishedTime - startTime,         rawShiftsFinishedTime - assignedShiftsFinishedTime,         unionFinishedTime - rawShiftsFinishedTime);     startTime = std::clock();     for (register unsigned __int32 forLoopTest = 0; forLoopTest < 0x008FFFFF; forLoopTest++)     {         testAssignedShifts();     }     assignedShiftsFinishedTime = std::clock();     for (register unsigned __int32 forLoopTest = 0; forLoopTest < 0x008FFFFF; forLoopTest++)     {         testRawShifts();     }     rawShiftsFinishedTime = std::clock();     for (register unsigned __int32 forLoopTest = 0; forLoopTest < 0x008FFFFF; forLoopTest++)     {         testUnion();     }     unionFinishedTime = std::clock();     std::printf(         'Execution time for assigned shifts: %08u clocks\n'         'Execution time for raw shifts:      %08u clocks\n'         'Execution time for union:           %08u clocks\n\n'         'Finished. Terminate!\n\n',         assignedShiftsFinishedTime - startTime,         rawShiftsFinishedTime - assignedShiftsFinishedTime,         unionFinishedTime - rawShiftsFinishedTime);      system('pause');     return 0; }  void testAssignedShifts() {     std::string invalidClsids('This is a test string');     std::vector<BYTE> compressedBytes;     DWORD invalidLength = (DWORD) invalidClsids.length();     compressedBytes.push_back((BYTE)  invalidLength);     compressedBytes.push_back((BYTE) (invalidLength >>= 8));     compressedBytes.push_back((BYTE) (invalidLength >>= 8));     compressedBytes.push_back((BYTE) (invalidLength >>= 8)); } void testRawShifts() {     std::string invalidClsids('This is a test string');     std::vector<BYTE> compressedBytes;     DWORD invalidLength = (DWORD) invalidClsids.length();     compressedBytes.push_back((BYTE) invalidLength);     compressedBytes.push_back((BYTE) (invalidLength >>  8));     compressedBytes.push_back((BYTE) (invalidLength >>  16));     compressedBytes.push_back((BYTE) (invalidLength >>  24)); }  typedef union _choice {     DWORD dwordVal;     BYTE bytes[4]; } choice;  void testUnion() {     std::string invalidClsids('This is a test string');     std::vector<BYTE> compressedBytes;     choice invalidLength;     invalidLength.dwordVal = (DWORD) invalidClsids.length();     compressedBytes.push_back(invalidLength.bytes[0]);     compressedBytes.push_back(invalidLength.bytes[1]);     compressedBytes.push_back(invalidLength.bytes[2]);     compressedBytes.push_back(invalidLength.bytes[3]); } 

Running this a few times results in:

Execution time for assigned shifts: 00012484 clocks Execution time for raw shifts:      00012578 clocks Execution time for union:           00013172 clocks  Execution time for assigned shifts: 00012594 clocks Execution time for raw shifts:      00013140 clocks Execution time for union:           00012782 clocks  Execution time for assigned shifts: 00012500 clocks Execution time for raw shifts:      00012515 clocks Execution time for union:           00012531 clocks  Execution time for assigned shifts: 00012391 clocks Execution time for raw shifts:      00012469 clocks Execution time for union:           00012500 clocks  Execution time for assigned shifts: 00012500 clocks Execution time for raw shifts:      00012562 clocks Execution time for union:           00012422 clocks  Execution time for assigned shifts: 00012484 clocks Execution time for raw shifts:      00012407 clocks Execution time for union:           00012468 clocks 

Looks to be about a tie between assigned shifts and union. Since I’m going to need the value later, union it is! Thanks!

Billy3

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:33:09+00:00Added an answer on May 11, 2026 at 3:33 pm

    Just use a union:

    assert(sizeof (DWORD) == sizeof (BYTE[4]));   // Sanity check  union either {     DWORD dw;     struct {          BYTE b[4];     } bytes; };  either invalidLength; invalidLength.dw = (DWORD) invalidClsids.length(); compressedBytes.push_back(either.bytes.b[0]); compressedBytes.push_back(either.bytes.b[1]); compressedBytes.push_back(either.bytes.b[2]); compressedBytes.push_back(either.bytes.b[3]); 

    NOTE: Unlike the bit-shifting approach in the original question, this code produces endian-dependent output. This matters only if output from a program running on one computer will be read on a computer with different endianness — but as there seems to be no measurable speed increase from using this method, you might as well use the more portable bit-shifting approach, just in case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Remove the :all in the before block fixes the issue.… May 11, 2026 at 8:06 pm
  • Editorial Team
    Editorial Team added an answer I studied the framework code a bit, and if initial… May 11, 2026 at 8:06 pm
  • Editorial Team
    Editorial Team added an answer Given that you have a lot of data points of… May 11, 2026 at 8:06 pm

Related Questions

I am writing a piece of code that to work would require an extensive
I am writing an application that needs to send data over a network. I
I am writing an assignment in MASM32 Assembly and I almost completed it but
I am wondering if there are any additional optimizations I can implement to improve
I am writing a project in C++ for an embedded system with no OS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.