473,699 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is my code measuring speed correctly?

31 New Member
I'm using clock() to measure the speed of my code (testing the speed of passing by value vs passing by reference in function calls). The problem is, the speed returned by my code is always 0.0000000

Can you check if I'm using the clock() function correctly?

the function is finding the number in the array that is closest to a number given by the user. Here is the code for the pass by value function.

Thanks for the help!

relevant code:
Expand|Select|Wrap|Line Numbers
  1. double Array::closestNumber (double num) {
  2.     double closest = 0;
  3.     int decimalPlaces = 15;
  4.     clock_t start;
  5.     clock_t end;
  6.     double speed;
  7.  
  8.     start = clock();
  9.     for (int i = 0; i < this->length; i++) {
  10.         if (fabs(this->contents[i] - num) < fabs(closest - num)){
  11.             closest = this->contents[i];
  12.         }
  13.     }
  14.     end = clock();
  15.  
  16.  
  17.     speed = ((double)end-(double)start)/CLOCKS_PER_SEC;
  18.     cout << "The time elapsed is: " << endl;
  19.     cout << fixed << setprecision(decimalPlaces) << speed <<endl;
  20.  
  21.     return closest;
  22. }
  23.  
Sep 30 '07 #1
11 2228
Firecore
114 New Member
This is the first time i heard of the clock function.
I did a google search and look at what came up.
Go here

I hope this helps.
Sep 30 '07 #2
blackx
31 New Member
thanks for the reply, yah, I saw that page also. clock() is from the ctime class.

I kinda got the idea for my code from here:
http://forums.macrumor s.com/showthread.php? t=360028
Sep 30 '07 #3
Savage
1,764 Recognized Expert Top Contributor
Have you seen this thread?

Savage
Sep 30 '07 #4
blackx
31 New Member
no I haven't. Thanks for pointing it out! I'll check it and come back here if I have some questions. Thanks again!
Sep 30 '07 #5
blackx
31 New Member
okay, I'm back here after reading the thread :)

I was told to specifically use the clock() function to measure speed. so I couldn't use the info on the other thread.

Any help would be appreciated. Thanks!
Sep 30 '07 #6
Ganon11
3,652 Recognized Expert Specialist
It looks like everything is in order...except I usually don't cast the clock_t variables to doubles. Try:

Expand|Select|Wrap|Line Numbers
  1. speed = (end - start) / CLOCKS_PER_SEC;
If this still returns 0, consider the fact that your array might be so small that going though it takes very little time.
Sep 30 '07 #7
Ganon11
3,652 Recognized Expert Specialist
Also, I think I see an error in your logic. When you set closestNumber to 0 initially, there is a chance that this value - 0 - might be the closest to the number, and it isn't even in the array (necessarily)! Try setting closestNumber to the first element in the array, and then checking it against every other element.
Sep 30 '07 #8
blackx
31 New Member
It looks like everything is in order...except I usually don't cast the clock_t variables to doubles. Try:

Expand|Select|Wrap|Line Numbers
  1. speed = (end - start) / CLOCKS_PER_SEC;
If this still returns 0, consider the fact that your array might be so small that going though it takes very little time.
Hi thanks for the reply.

I got the code to work...it seems the reason why I always got 0 IS because the array is too small. So what I did was ran a loop for 10000 times to add up all the speed times, only then did I got some solid numbers to differentiate.

2 related questions:
1) It seems based on my results that the pass by reference takes longer than pass by value for function calls. This does not seem intuitive to me at all. Can anyone explain why I am getting this result?

2) I was trying to do a pass by constant reference

and I ended up having to use:
Expand|Select|Wrap|Line Numbers
  1. double closestNumber (const double& num) const;
  2.  
instead of just:
Expand|Select|Wrap|Line Numbers
  1. double closestNumber (const double& num);  //no const in the end
  2.  
If I didn't put a const in the end, I get a compile error (ambigious call to overloaded function error c2668). I just would like to understand why I need to use the first type (with the const in the end) for my code to work and what's the difference between the two. I'm still kinda confused with the use of const.

Thanks for all the help.
Sep 30 '07 #9
Ganon11
3,652 Recognized Expert Specialist
When you added the const double& num function, did you get rid of the previous one with double& num in the function header? If not, then you have two functions with very similar signatures. When you call closestNumber(a Number), which one gets called!?

Though I'm not sure how adding the 'const' keyword to the function header alleviates the problem, I do know that with that const keyword, you are saying "This function cannot and does not change anything within the class."
Sep 30 '07 #10

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

Similar topics

20
3888
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes are pre-calculated and passed to the query. SELECT * FROM Locations
4
3023
by: D. Alvarado | last post by:
Hello, I would like to design a page that measures the user's download connection. Does anyone have an example link or script that might aid me in this task? Thanks, - Dave
10
1902
by: saraca means ashoka tree | last post by:
The following code is the heart of a program that I wrote to extract html tags from a webpage. How efficient is my code ?. Is there still possible way to optimize the code. Am I using everything as per the text book. I am just apprehensive whether this may break or may cause a memmory leak. Any chance for it. #define TOKN_SIZE 256 void tagfinder() {
24
8619
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I have used. Original code : c= 0 ; for (i=0; i<999; i++)
12
1641
by: Zach | last post by:
Hi, I am making changes to a few modules of code and wish to test to see if my changes have made any increases in memory utilization, execution speed etc... The code is part of a server so lots of other stuff has to be run AFAIK to test my code (all the server modules). Anyone know exactly how I can test my code? Ideally I can make a writeup which says something like, "My changes resulted in functions foo and bar showed a N% reduction...
5
9969
by: =?Utf-8?B?QW5kcmV3IEhheWVz?= | last post by:
You've all seen them. Hoards of sites (predominantly ISP's) offering to test your bandwidth to the four corners of the globe, all trying to show that they are faster and you should switch to using them. Well, I now have a need to implement something similiar within my ASP.NET applications so that I can troubleshoot slow performance problems between our clients and our servers. An ActiveX control or Java Applet that the client can...
7
2425
by: Wisgary | last post by:
I'm doing some benchmarking tests to compare Microsoft's CLR against Mono's CLR. I could use some suggestions for how to objectively compare the code. To my surprise the few tests I've run so far have had Mono running quite a bit faster than Vanilla .NET on my Windows XP installation, which has me wondering if I'm doing something that's sandbagging .NET. It's all console code, for Microsoft I'm just running the .exe file from the command...
20
1973
by: upperclass | last post by:
Hi, I'm trying to find a decent way of measuring the running time of a long-running program. Say, the running time ranges from several seconds to more than a day. The standard clock() function seems inadequate for this task. (I read somewhere in this group that clock_t would likely overflow after <1 hour).
19
3798
by: Pavan | last post by:
Hi, I want to know if there is any software for measuring lines of code of my c++ application. I found out a tool, sloccount, but it gives only physical lines of code. I found out one more tool cccc , but iam getting many parse errors with it. If you know any other tool( to be used in linux) please let me know.
0
8686
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
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9173
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
9033
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8911
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,...
1
6533
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...
1
3057
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
2345
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2009
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.