Jump to content

Is using a Pointer faster?


WolfWorld
 Share

Recommended Posts

I just started c++ a day ago >_<

okay here the question

For example I have two script written.(Using C++ Builder)

String myString = "Hello World", * myPointer = &myString;

myPointer->length(); //This or

myString.length(); //This

Will be faster if I do it in a loop?

I don't know how to measure time set so.

@athiwatc in the interest of helping I’ll make some suggestions and hope to avoid the current discussion.

Firstly: to the questions which is faster? I have not idea but I will say cleaner is better. If Nutster, Valik and Ascend4nt, who I believe know much more than I, are still “discussing it” then I think it’s not worth worrying about, Wus is right. My guess would have been myString.length() because it is a local var but is just a guess. I don’t really care as long as it works!

Secondly: This is more my point. You just started learning C++ which is great! You will have many many questions which is great too. Thinking to when I was learning, the best way to learn about pointers and arrays is looking at pictures. There is so much to learn and over time the pieces fall into place, you can’t expect to understand the answer to your questions with so little knowledge. Valik has posted some good books/references here - http://www.autoitscript.com/forum/index.php?showtopic=41358 and there is always Google. It is very hard to explain complicated stuff with just text. My lecture always insisted to get out pen and paper.

Thirdly: you don’t have to know all the answers! They will come in time as try stuff out and get stuff wrong. See if you can find someone you know who can teach or guide you as your learn.

My 2 cents but I hope that helps :(

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

Let me make something clear to the idiot on the other side of this conversation - I didn't ONCE tell anyone to use one technique over another. And AGAIN I told them repeatedly that the performance difference would be negligible or nearly microscopic. You keep missing that point.

And you're reaching here by thinking an explanation/answer/response to your asinine remarks is an 'excuse', or that I'm trying to say in some way I'm the one with the "correct" response to the original post. Right now I'm just trying to get some points across that just aren't sinking in, though I assume you'll twist the actual fact that I responded into something else.

But hey, keep hammering away all you want, you're not going to actually win a prize here.

*edit: just so noone gets confused, this was addressed to Valik, not the poster above me

Edited by Ascend4nt
Link to comment
Share on other sites

Let me make something clear to the idiot on the other side of this conversation - I didn't ONCE tell anyone to use one technique over another. And AGAIN I told them repeatedly that the performance difference would be negligible or nearly microscopic. You keep missing that point.

And you're reaching here by thinking an explanation/answer/response to your asinine remarks is an 'excuse', or that I'm trying to say in some way I'm the one with the "correct" response to the original post. Right now I'm just trying to get some points across that just aren't sinking in, though I assume you'll twist the actual fact that I responded into something else.

So, why shouldn't I ban you for the quoted text? Why shouldn't I ban you simply on principle since you clearly have not learned your lesson from the first time I banned you?

But hey, keep hammering away all you want, you're not going to actually win a prize here.

The same could be said of you. Quite literally, your future on this forum hangs in the balance here. You've just demonstrated exceedingly well that you are incapable of learning from your past mistakes. You know my history and my temperament first-hand going into this yet you not only continue your foolish debate you actually devolve into insulting me. You're so convinced you are right that you fail to realize a number of very obvious things. Chief among them is your glaring hypocrisy in just about everything you say to me. You accuse me of missing your point(s) (I do not miss them, however, I do find your points irrelevant or uninteresting) you clearly miss my points. For example, here's a small example of the obviousness you seem unable to grasp:

And AGAIN I told them repeatedly that the performance difference would be negligible or nearly microscopic. You keep missing that point.

I do not keep missing that point, I think you miss your own point. If it's such a microscopic gain... why bother talking about it at all? I have already stated I myself realize how insignificant and irrelevant the entire discussion is except for theoretical fun. A point you clearly miss. A second part of that is who is the "them" you refer to? The original poster? Yes, I'm sure the original poster is not at all confused by any of the techno-babble you and David went into. Or is it easier to think of things in terms of your experience and forget that somebody on day two of their journey doesn't know what they should and should not listen to? Is your discussion meant for the rest of us, most of which don't care or don't understand it? Or is it meant for the C++ Professor who's likely forgotten more about programming than half the forum knows?
Link to comment
Share on other sites

@Ascend4nt and Valik

At the risk of being getting into trouble I wanna say this. This is the second time I have seen a "discussion" between you two and I have seen Valik "discuss" with many others myself included.

IT DOES NOT MATTER WHO IS RIGHT!!! It matters on what is right. Accepting you are not going to agree is probably the right thing to do here. You both put your points of view across which are interesting to read, you may not agree but in 5 years who is going to care? When both parties insist they are correct, no about of “discussion” is going solve it. When it gets into a pissing contest it comes down to one person has power to ban and the other doesn't and no one wins.

Just my opinion, take it or leave it.

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Link to comment
Share on other sites

Okay, that's actually something I was curious about - whether you could handle being called the same thing you called me, twice ('idiot'). Yet of course, it's just me who devolved into insults, not an innocent moderator.

As far as the rest of the stuff - we're going in circles here. You think discussing something was wrong, I don't. The rest of it has been covered - reread things if you want to see who I addressed regarding what.

If you ban me, there's nothing I can do about it. But if you do, please delete all the code I've posted, it'd look better if people thought I was just a useless idiot who never contributed to the community. (I'm guessing that's your opinion)

Link to comment
Share on other sites

Okay, that's actually something I was curious about - whether you could handle being called the same thing you called me, twice ('idiot'). Yet of course, it's just me who devolved into insults, not an innocent moderator.

I suggest you go back and re-read this thread. You certainly are an idiot but it was you who began the pissing match by putting words in my mouth. Here, let me save you the trouble:

Yes Valik, everyone who doesn't side with you must be 'stupid',

Did I call you an idiot in response to that? Yes. Yes I did, because you are an idiot for putting words in my mouth. I'm quite capable of calling you stupid without you attempting to do it for me. Thanks.

As far as the rest of the stuff - we're going in circles here. You think discussing something was wrong, I don't. The rest of it has been covered - reread things if you want to see who I addressed regarding what.

Wrong. And this is the crux of my assertion that you miss my point: I do not think your discussion was wrong. I think the way you tried to justify your discussion was wrong. I don't care if you and David wish to talk about the assembly details of various common C++ constructs. But I draw the line when you attempt to needlessly justify your discussion with flimsy arguments. You hypocritically questioned the relevancy of my idle remark. I responded by pointing out your hypocrisy that your discussion was equally as irrelevant to the original question. From there you've clearly lost the plot about what my point is.

If you ban me, there's nothing I can do about it. But if you do, please delete all the code I've posted, it'd look better if people thought I was just a useless idiot who never contributed to the community. (I'm guessing that's your opinion)

Whether or not you are banned, I will not delete your posts regardless of what I think about you.
Link to comment
Share on other sites

Judging by David's answer, Wus' comments and my own personal thoughts, I think we are all in agreement that providing a literal answer to the original question is stupid and certainly the wrong thing a new programmer should care about.

Can't get out of that one, even if it's indirect. The context is clear.

Regarding the rest - of course, Valik is right (as he always is) - I've 'lost the plot', am a 'hypocrite' and missed his point. None of those apply to him.

There you go, you win a prize after all.

*edit: fixed code to quote tags

Edited by Ascend4nt
Link to comment
Share on other sites

Can't get out of that one, even if it's indirect. The context is clear.

There is a difference between calling you stupid and saying you said something stupid. People say stupid things. I can give any number of examples of otherwise intelligent people saying something stupid on this forum.

Regarding the rest - of course, Valik is right (as he always is) - I've 'lost the plot', am a 'hypocrite' and missed his point. None of those apply to him.

I think you need a week away to think about this thread. Something to think about is figuring out how to actually backup what you say. It's one thing to sit around and accuse one another of being hypocrites but it's another thing entirely to demonstrate it. I've gone out of my way to cite various examples of your hypocrisy and all you can do in return is make accusations you do not support.

Also, based on past experience with you, I'm going to make this clear right now. Do not PM me. I'm not really interested in discussing this further. I've given you plenty of opportunities to defend yourself and I even let you hang around after you called me an idiot, which I suspect many thought would be the point of no return for you. You've squandered your attempts at reasonable discourse. But, of course, it was all my fault and you are totally exempt from any personal accountability in the whole affair.

Link to comment
Share on other sites

Okay athiwatc, back to the original question. I want to make clear my original points, or at least the ones I intended to make.

  • It is generally more important in class and in business to write clean, easy to follow, easy to debug code, rather than super-optimal code that no one understands, even yourself a few months later.
  • Generally the less indirection used, the better. It makes following your code easier and makes things a little faster. Also, it reduces the number of ways you can introduce bugs.
  • When passing large data structures to a function, it is generally better to use a reference (or a pointer if you have to), than copy the whole object. This reduces the amount of data to be copied and saves calling your object's constructor function.
Notice that I said "generally" in each point. This is because for each point, there are specific situations where following this advice is not the best way to do things. As you progress in your education and your experience with C++ increases, you may find situations where it is better to ignore this advise, but when you do, have a specific reason to do so.

With the speed of modern processors and the strength of modern optimizers, tweaking little things at this level is likely to be more hassle than it is worth and just make your code more difficult to follow and maintain. That was what my point with the two versions of strcmp was meant to illustrate, but I guess I missed the mark there somewhere.

Also, working with pointers can be tricky. If you are not careful using them, you can crash your program and, in extreme cases, crash your computer, by using pointers carelessly or incorrectly. Make sure you understand the rest of the language before tackling this advanced topic. That said, there are many situations where pointers are exactly the mechanism you want to use. But learn to walk before running.

BTW, you were not off-topic; someone else was, but not you. >_<

Edited by Nutster
Add more detail and clarity (I hope)

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

In summary: pointer dereferencing will probably only ever be faster (in the general sense) for fixed C++ datatypes (int, char, etc). Otherwise, with structures/classes, I don't see your code making a difference

*edit: regarding #2 - actually, for structures/classes I don't know how C++ would get the next item, that's actually got me wondering now...

That is the point, am talking about String(array of char) here. There is almost no point in using pointer on int as it's almost the same size(or the same?)

Then till now I spend most of my time studying c++ :(

Okay, from reading -> this <- , its clear that creating arrays of structures\classes in C++ is going to cause a 'disaster' when calling a function that works on an 'parent' object array, with a derived class. So, it looks like arrays better be used only for the basic data types.. something I hadn't thought to look into until now!

I came across vector a lot of time but most of the time it got the deal with string so this is VERY interesting! vector with int. Next am going to study vectors function.

To start I'd like to say that this thread is a good read -- it is exposing me to a few new things which is always good.

Having said that, my problem with the current discussion is that it seems like the original poster's post has not really been properly addressed. He started with

and then went on to ask about how to squeeze an extra few percentage points of efficiency out of c++ code. Perhaps I am reading this wrong, but my response is to say 'WTF!' There is absolutely no reason for him to be worried about the number of clock cycles it takes to de-reference a pointer. IMO entertaining this question in this manner is quite counterproductive for the OP.

To finish...

@athiwatc

The fact that you asked this question shows that you are thinking which is fantastic. Unfortunately you are thinking too far ahead at the moment -- you are running before you can walk. Stop trying to optimize code and just try to learn the language. I'd suggest that you read this thread as no more than an abstract aside.

Wait a second, I said I started with c++ a day ago. That does not mean I don't know how all of this works. I just never used a pointer before I came across c++, I've program in Delphi and Javascript for almost 2-3 years now. Autoit almost 1-2 year can't remember. And Auto Hotkey(Which I realize later that it's a total rip off of Autoit V2) before Autoit V3 a year, HTML almost let's say 9 years old. Now am 16 going to be 17 :(

Okay athiwatc, back to the original question. I want to make clear my original points, or at least the ones I intended to make.

  • It is generally more important in class and in business to write clean, easy to follow, easy to debug code, rather than super-optimal code that no one understands, even yourself a few months later.
  • Generally the less indirection used, the better. It makes following your code easier and makes things a little faster. Also, it reduces the number of ways you can introduce bugs.
  • When passing large data structures to a function, it is generally better to use a reference (or a pointer if you have to), than copy the whole object. This reduces the amount of data to be copied and saves calling your object's constructor function.
Notice that I said "generally" in each point. This is because for each point, there are specific situations where following this advice is not the best way to do things. As you progress in your education and your experience with C++ increases, you may find situations where it is better to ignore this advise, but when you do, have a specific reason to do so.

With the speed of modern processors and the strength of modern optimizers, tweaking little things at this level is likely to be more hassle than it is worth and just make your code more difficult to follow and maintain. That was what my point with the two versions of strcmp was meant to illustrate, but I guess I missed the mark there somewhere.

Also, working with pointers can be tricky. If you are not careful using them, you can crash your program and, in extreme cases, crash your computer, by using pointers carelessly or incorrectly. Make sure you understand the rest of the language before tackling this advanced topic. That said, there are many situations where pointers are exactly the mechanism you want to use. But learn to walk before running.

BTW, you were not off-topic; someone else was, but not you. >_<

I was asking this because I am going to make a encryption script for my own fun, Yes FUN!

Anyway stop fighting please

Edited by athiwatc
Link to comment
Share on other sites

Anyway stop fighting please

Unfortunately, some people come to the party just to cause trouble. So the host of the party and/or some trusted associates (the moderators) gently tell the trouble-makers to behave properly or be shown the door, which is what happened here. It is in everyone's best interest to keep things sane around here, rather than degenerate to chaos. I would much rather see a few bouncers at a party than let the thing get totally out of control. Okay, so Valik goes after people with a metaphorical baseball bat, but in all the years we have been here, I have yet to see him mis-fire it on someone that did not deserve it.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Unfortunately, some people come to the party just to cause trouble. So the host of the party and/or some trusted associates (the moderators) gently tell the trouble-makers to behave properly or be shown the door, which is what happened here. It is in everyone's best interest to keep things sane around here, rather than degenerate to chaos. I would much rather see a few bouncers at a party than let the thing get totally out of control. Okay, so Valik goes after people with a metaphorical baseball bat, but in all the years we have been here, I have yet to see him mis-fire it on someone that did not deserve it.

>_<

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...