Solved

Question on C code, how to make this code more efficient

Posted on 2014-02-23
2
964 Views
1 Endorsement
Last Modified: 2014-02-27
Hi Guys,

I'm helping a friend making their C code more efficient for  encryption program. Do you guys have any ideas on what i can do/modify to make it more efficient and improve on the current code?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>

typedef int int32;
typedef unsigned char int8;

static void combine_it(int8  *output, int8  *input_1, const int8  *input_2, int32 length)
{
   int32 i = 0;

   for(i=0; i<length; i++)
   {
       output[i] = (int8)(input_1[i] ^ input_2[i]);
   }

   return;
}

static void itoa( int32 num, int8  *alpha, int32 radix )
{
   if( radix == 10 )
   {
       sprintf(alpha, "%i", num);
   }
   else if( radix == 16 )
   {
       sprintf(alpha, "%X", num);
   }
}

int8 *create_new_key(int8 modifier, const int8  *input_1, int32 length)
{
   int8  leading[3];
   int32 i_leading;
   int8 * temp_string = NULL;
   int8 * ret;
   int32 i = 0;

   itoa(modifier/2, leading, 10);
   i_leading = atoi(leading);
   temp_string = (int8 *) malloc(8);
   ret = (int8 *) malloc(length);
   memset(temp_string, 0, 8);
   temp_string[0] = 0; 

   if( (modifier+1)%2 == 0 ) {
       temp_string[0] = (int8)((i_leading<<4) + 8);
   }   
   else {
       temp_string[0] = (int8)(i_leading<<4);
   }   

   for(i=0; i<(length>>3); i++)
   {
       combine_it(ret+i*8, temp_string, input_1+i*8, 8);
   }   
   free(temp_string);
   return ret;
}   

// Do not change anything in main
// main only contains example input
// Change anything else
int main(int argc, char **argv) {

   // Example input data
   int8 data[32] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,
                    17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
   int8 *resp = create_new_key(0x10, data, sizeof(data));
   free(resp);

   return 0;
}

Open in new window

1
Comment
Question by:Tbalz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 250 total points
ID: 39880804
Hi TBalz,

Are you experiencing a particular performance issue that you'd like resolved?

Today's compilers are very good at optimizing program source code into executable code.  You could rewrite the loops to use pointers and pointer arithmetic instead of integer arithmetic, but I doubt you'll see a difference.  Turn on the compiler optimizations to the fullest sill probably get you fast code than you can write yourself.


Kent
0
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 39880884
Your combine_it() function is called once in a loop. You can replace the combine_it() function with just the code and remove the overhead of passing in 4 arguments.

Also, as kdo suggested, you can replace subscripting by using pointers directly.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question