473,804 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Efficiency of string parsing

92 New Member
I know this sounds like a homework problem, but it's honestly not one. I am writing some code which given an IP address in string format will calculate the integer value of it. Now the trick is I need it to be quick. I see two paths and not sure which would be faster.

1) Use strtok like functionality (actually custom code to make it reentrant and in my case I can change the string) to parse the components of the address. I would also keep a count of the components for error checking.

2) Use sscanf (ip_addr_str, "%d.%d.%d.% d", &c1, &c2, &c3, &c4) and then check the return value to ensure proper formatting (ie 4 components).

Any ides which would be quicker? Any other ideas? My next step is to experiment but was hoping someone could help me avoid writing code multiple times to play with.
Jul 4 '07 #1
2 1565
gpraghuram
1,275 Recognized Expert Top Contributor
I know this sounds like a homework problem, but it's honestly not one. I am writing some code which given an IP address in string format will calculate the integer value of it. Now the trick is I need it to be quick. I see two paths and not sure which would be faster.

1) Use strtok like functionality (actually custom code to make it reentrant and in my case I can change the string) to parse the components of the address. I would also keep a count of the components for error checking.

2) Use sscanf (ip_addr_str, "%d.%d.%d.% d", &c1, &c2, &c3, &c4) and then check the return value to ensure proper formatting (ie 4 components).

Any ides which would be quicker? Any other ideas? My next step is to experiment but was hoping someone could help me avoid writing code multiple times to play with.
If your job is to convert the ip address to integer value then you can use the already existing function inet_addr

Raghuram
Jul 4 '07 #2
Benny the Guard
92 New Member
I had thought of that but have a few issues:

1) It does not check for completeness. I need to check that all octets are specified, inet_addr does not do this.

2) I need an unsigned int value, not sure how this equates to in_addr_t

3) Not sure inet_addr checks the octets being bound 0-255. But maybe it does.
Jul 4 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2142
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more efficient than repeatedly referencing the member in a loop? Maybe using a string isn't the best example, but hopefully you get the idea! * example (referencing member):
26
808
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i don´t have a lot of success. Can anybody help me (and understand my english :-))? I have a .txt-file in which the data is structured in that way: Project-Nr. ID name lastname 33 9 Lars Lundel 33 12 Emil Korla
100
3656
by: jacob navia | last post by:
As everybody knows, C uses a zero delimited unbounded pointer for its representation of strings. This is extremely inefficient because at each query of the length of the string, the computer starts an unbounded memory scan searching for a zero that ends the string. A more efficient representation is: struct string {
9
14125
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6). so i wonder is there any faster way to parse string(char array) into float number.
12
2318
by: PD | last post by:
I am currently making a dating website. I want to have some information on how to structure the database and the php files so that I can achieve speed and efficiency. Can some one please give me suggestions and point me to references where I can get this information.
19
2936
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
9
3321
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm not concerned when dealing with user typing, but I am if a procedure is called by a query. Does the VBA compiler generate "in-line" code for some apparent function calls? For example, y = Abs(x) might be compiled as y = x & mask. The string...
3
2089
by: Benny the Guard | last post by:
I have a comma delimited file to parse up. So I figured I will go line by line and parse them up. Now efficiency is the top priority here. So I figure I can use the old-school C approach or use std::string. The way I see it I could (assuming infile is a valid std::ifstream) char buffer ; while (!infile.eof () && !infile.bad ()) { // Get the next line in the file infile.getline (buffer, 4095); // If fail bit is set it means we...
1
4411
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or Stack code and parsing code my professor i does not like me using buffer reader on my code and my professor did even give me an example code for parsing as well as pop push top or Stack code and i don't know how to do this code into parsing and pop push...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10569
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.