473,795 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[XPOST] [C] Access speed: pointers Vs array

[xpost and followup-to: comp.os.linux.d evelopment.apps]

Hi to everyone! ;)

In C language on Linux, what string access method is faster?

1) Puntatori
char str[]="hello";
char* pstr;
pstr = str;
// access such as "*pstr"

2) Array
char str[]="hello";
// access such as "str[i]"

To answer this question, and inspired by
<ce************ **************@ posting.google. com>, i wrote a (very
simple!) code [1] that calculates the string access time based on the
previous two methods (in different implemented versions of strcpy).

My platform was:
- gcc compiler
- Kernel linux 2.4
- Knoppix 3.2
- Hardware intel Pentium 4 M 1.8 Ghz, 256 MB RAM

RESULT: the array access method seems faster!

What are your comments?
[1]CODE
#include <stdio.h>
#include <string.h>
#include <time.h>

strcopy1(char s1[],char s2[])
{
int x;
for (x=0; x<=strlen(s2); x++)
{
s1[x]=s2[x];
}
}

strcopy2(char s1[],char s2[])
{
int x;
int y = strlen(s2);
for (x=0; x<=y; x++)
s1[x]=s2[x];
}

strcopy3(char s1[],char s2[])
{
int x;
int y = strlen(s2);
for (x=0; s2[x]==0; x++)
s1[x]=s2[x];
}

strcopy4(char* s1, char* s2)
{
while(*s2 != '\0')
{
*s1 = *s2;
s1++;
s2++;
}
}

strcopy5(char* s1, char* s2)
{
while(*s2)
*s1++=*s2++;
}

strcopy6(char* s1, char* s2)
{
while(*s1++=*s2 ++);
}
main(int argc, char *argv[])
{

double interval;
time_t start;
time_t finish;
char* pstring1;
char* pstring2;
long int i = 1;
const int bound = 10000000;
char string2[] = "I'm testing the speed of execution of strcpy. I'm
testing the speed of execution of strcpy. I'm testing the speed of
execution of strcpy.";
char string1[200];

pstring1 = string1;
pstring2 = string2;
//STRCOPY1
time(&start);
while (i<bound)
{
strcopy1(string 1,string2);
i++;
}
time(&finish);

interval = difftime(finish ,start);
printf("\n");
printf("STRCPY1 : ");
printf("%f", interval);
printf("\n");

string1[0]='\0';
i=1;

//STRCOPY2
time(&start);
while (i<bound)
{
strcopy2(string 1,string2);
i++;
}
time(&finish);

interval = difftime(finish ,start);
printf("\n");
printf("STRCPY2 : ");
printf("%f", interval);
printf("\n");

string1[0]='\0';
i=1;

//STRCOPY3
time(&start);
while (i<bound)
{
strcopy3(string 1,string2);
i++;
}
time(&finish);

interval = difftime(finish ,start);
printf("\n");
printf("STRCPY3 : ");
printf("%f", interval);
printf("\n");

string1[0]='\0';
i=1;

//STRCOPY4
time(&start);
while (i<bound)
{
strcopy4(pstrin g1,pstring2);
i++;
}
time(&finish);

interval = difftime(finish ,start);
printf("\n");
printf("STRCPY4 : ");
printf("%f", interval);
printf("\n");

string1[0]='\0';
i=1;

//STRCOPY5
time(&start);
while (i<bound)
{
strcopy5(pstrin g1,pstring2);
i++;
}
time(&finish);

interval = difftime(finish ,start);
printf("\n");
printf("STRCPY5 : ");
printf("%f", interval);
printf("\n");

string1[0]='\0';
i=1;

//STRCOPY6
time(&start);
while (i<bound)
{
strcopy6(pstrin g1,pstring2);
i++;
}
time(&finish);

interval = difftime(finish ,start);
printf("\n");
printf("STRCPY6 : ");
printf("%f", interval);
printf("\n");;

string1[0]='\0';
i=1;

//STRCOPY7
time(&start);
while (i<bound)
{
strcpy(pstring1 ,pstring2);
i++;
}
time(&finish);

interval = difftime(finish ,start);
printf("\n");
printf("STRCPY7 : ");
printf("%f", interval);
printf("\n");;

string1[0]='\0';
i=1;

}
OUTPUT
TIME(in seconds):
STRCPY1: 531.000000
STRCPY2: 12.000000
STRCPY3: 4.000000
STRCPY4: 7.000000
STRCPY5: 6.000000
STRCPY6: 10.000000
STRCPY7: 3.000000

--
Il Prof.
Nov 14 '05 #1
4 2082
On Thu, 05 Aug 2004 15:46:02 +0200, Il Prof
<ch*********@ch iedisulng.org> wrote in comp.lang.c:
[xpost and followup-to: comp.os.linux.d evelopment.apps]


*plonk* for rudeness.

Who are you to decide that posters in comp.lang.c are entitled to
answer your questions and not even see their own posts, especially if
they don't notice that you trimmed comp.lang.c from the follow-ups?

Your question is off-topic anyway. The C language doesn't define the
relative speed of *ANYTHING*, and it doesn't define Linux at all.
This is not a language question, it is a gcc compiler implementation
question.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
Jack Klein wrote:
Il Prof <ch*********@ch iedisulng.org> wrote in comp.lang.c:
[xpost and followup-to: comp.os.linux.d evelopment.apps]
*plonk* for rudeness.

Who are you to decide that posters in comp.lang.c are entitled to
answer your questions and not even see their own posts, especially if
they don't notice that you trimmed comp.lang.c from the follow-ups?


I disagree. He even announced the follow-ups, and I consider that
having the original poster set follow-ups to one newsgroup goes a
long way towards killing these long off-topic threads. In fact I
applaud his action there.
Your question is off-topic anyway. The C language doesn't define
the relative speed of *ANYTHING*, and it doesn't define Linux at
all. This is not a language question, it is a gcc compiler
implementation question.


And I agree here. This should never have appeared on c.l.c in the
first place.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski
Nov 14 '05 #3
Jack Klein <ja*******@spam cop.net> wrote:
[xpost and followup-to: comp.os.linux.d evelopment.apps]
*plonk* for rudeness.


Also you can't read my message...
Who are you to decide that posters in comp.lang.c are entitled to
answer your questions and not even see their own posts, especially if
they don't notice that you trimmed comp.lang.c from the follow-ups?
There was, as netiquette docet, the xpost&f/up warning.
Your question is off-topic anyway.


Sorry, i will not post other messages on this topics here.

Regards.

--
Il Prof.
Nov 14 '05 #4

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:b1******** *************** *********@4ax.c om...
Your question is off-topic anyway. The C language doesn't define the
relative speed of *ANYTHING*, and it doesn't define Linux at all.
There are certainly C-language aspects to his question. For example, the
standard may permit certain assumptions on arrays that it doesn't permit on
pointers. As a simple example, two arrays can often be assumed not to
overlap in situations where two pointers couldn't.
This is not a language question, it is a gcc compiler implementation
question.


Where compiler implementation questions come down to what the standard
allows and what it doesn't, they are also C language questions. The question
asked is sufficiently broad that it could be answered from many different
points of views and persectives.

I give the OP the benefit of the doubt and suspect he added comp.lang.c
because he also wanted an answer from a C standard perspective. You notice
that neither Linux nor gcc were present in his choice of the subject line,
and many of his issues (bugs in the implementations , cases where the
standard required multiple function calls versus cases where it didn't)
could be addressed from a pure C language perspective.

After all, the next version of gcc may be implemented very differently,
but the C standard is not going to change.

DS
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4850
by: Steven James Samuel Stapleton | last post by:
Will calling ksort() on an array speed up it's access? For example, I have the array $file_index, which is accessed by a key (the entry id) and has two sub elements in a one dimensional array (line number of the start of the entry in the file, and the byte offset of the start of the entry). so, I might have an array that can be represented thus: {
5
3537
by: ArShAm | last post by:
Hi there Please help me to optimize this code for speed I added /O2 to compiler settings I added /Oe to compiler settings for accepting register type request , but it seems that is not allowed and if I remove register type for "l" , time of generating codes doesn't change the original code makes some files , but I removed that section to make it simple for you to read please help me to optimize it for faster running
4
3542
by: Paul Brown | last post by:
Thanks for the replies Tristan, Eric, Steven & Kurt. They have given me some good leads. I present justification for a lot of the comments that drew (constructive) criticism below. Firstly, let me summarise the feedback that has pointed me in the right direction :- >> Steven > 20.13: What's the best way of making my program efficient? > A: By picking good algorithms and implementing them carefully.
2
1791
by: SKarnis | last post by:
We are trying to rebuild a current Access 2002 (XP) mdb program to VB.NET with a SQL database - we are having problems with a suitable combobox. There are many threads discussing multiple column comboboxes in .NET. We are having success with the multiple columns similar to the combobox from Access 2002 (XP). Our biggest problem is speed. In a form in Access 2002, our combobox is able to load data in a table in separate Access database...
15
2354
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in computers memory? Or does Python hide such implementation details that deep, that there is no way to get down to them? The test code below shows, that extracting bits from an integer value n is faster when using n&0x01 than when using n%2 and I...
21
3814
by: Gary Bond | last post by:
Hi All, I am a bit stuck with a project: Specifically, when making a database like engine in 'the old days', I would have wrapped a record class with a stream class, so I could have a file of records on disc, such that I could always jump straight to the record number I wanted. Simple random file access. Maybe they were fixed length records and I knew the n'th record was (n*length of record) into the file. I could therefore jump...
33
7188
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
3
3046
by: Keith Thompson | last post by:
Victor <vhnguyenn@yahoo.comwrites: You're declaring an array of pointers to unsigned long long, but you're initializing the pointers with integer values. This is actually a constraint violation, and your compiler should have warned you about it. Your problem is either that you're invoking your compiler in some non-standard mode that inhibits the warnings, or getting warnings and not bothering to tell us about them.
1
2079
by: alansharp | last post by:
Hi guys Im attempting to write conways Game of life and need to use a pointer and 2 arrays. The reason im using a pointer is hopefully to speed up the code rather than copying the array > processing it and copying it back. I found this code in an old post on the forum but having never used pointers before im a little lost with what to do. int array = new int; fixed(int* pointer = &array) { //use the pointer
0
10436
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...
0
10000
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...
0
9040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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
6780
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
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.