<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Altentee » Performance &#38; Test Automation Experts &#187; management</title>
	<atom:link href="http://altentee.com/blogs/tag/management/feed/" rel="self" type="application/rss+xml" />
	<link>http://altentee.com</link>
	<description>Performance and Test Automation Experts</description>
	<lastBuildDate>Fri, 04 Nov 2011 03:17:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>A year in review and a consolidated approach</title>
		<link>http://altentee.com/blogs/2010/a-year-in-review-and-a-consolidated-approach/</link>
		<comments>http://altentee.com/blogs/2010/a-year-in-review-and-a-consolidated-approach/#comments</comments>
		<pubDate>Thu, 30 Dec 2010 21:53:02 +0000</pubDate>
		<dc:creator>Tim Koopmans</dc:creator>
				<category><![CDATA[Altentee]]></category>
		<category><![CDATA[jmeter]]></category>
		<category><![CDATA[loadrunner]]></category>
		<category><![CDATA[management]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[watir]]></category>

		<guid isPermaLink="false">http://altentee.com/blogs/?p=1144</guid>
		<description><![CDATA[Wow, wasn&#8217;t that long ago when I was promising things that were harder, better, faster and stronger. I think I got part of the way there. The core reason I set up Altentee was to provide reliable (and potentially cheaper) alternatives to traditional licensed performance testing tool sets. There&#8217;s no doubt Altentee can test at [...]]]></description>
			<content:encoded><![CDATA[<p>Wow, wasn&#8217;t that long ago when I was promising things that were <a href="http://altentee.com/blogs/blogs/2010/harder-better-faster-stronger-and-cheaper/">harder, better, faster and stronger</a>. I think I got part of the way there. The core reason I set up Altentee was to provide reliable (and potentially cheaper) alternatives to traditional licensed performance testing tool sets. There&#8217;s no doubt Altentee can test at the limits of your typical web app using tools that cost zilch. The bigger challenge I&#8217;ve found this year is convincing potential clients that they really don&#8217;t need to spend that much. I&#8217;ve even offered a <a href="http://altentee.com">free load test on our homepage</a> to help illustrate this point.</p>
<p>We were lucky to be selected by the development team at <a href="http://www.cordelta.com/">Cordelta</a> to help them automate and performance test a high profile public website called <a href="http://www.myhospitals.gov.au/">MyHospitals</a>. We were able to test millions of hits per hour from domestic and international locations in a wide variety of load scenarios. The success of this approach was underpinned by the following:</p>
<ol>
<li>An open minded project / development team not coupled to a &#8216;must-have-most-expensive-toolset-to-do-job&#8217; mentality</li>
<li>An open minded performance test analyst (me!) who believes Excel really is the grandpa of charting, R is the grandma of stats, Sparklines are the only way to present time series data to management, ANY tool can simulate load via HTTP/S and that there is no real distinction between good software testers or developers (only hard work separates the best from the worst).</li>
<li>That 2010 come-no-doubt 2011 buzz word&#8230; Cloud</li>
</ol>
<p>As I sit here pluggin&#8217; our alternative approach at Altentee, I can see the rise of other more <a href="http://browsermob.com">successful</a> <a href="http://loadstorm.com">punters</a> taking on the <a href="http://loadstorm.com/2010/price-right-loadrunner">big kids</a>. I sit and [continue to] chuckle at the reaction to LR pricing and LR zealots who will fall on their swords over LR itself. I have come to realise one thing though, it is not about the tool, or even the alternate tool like perhaps I first thought. It is more about the <span style="color: #008000;"><strong>freedom of choice</strong></span>.</p>
<p>To tackle MyHospitals I was free to choose and implement the following tools:</p>
<ol>
<li><a href="https://github.com/90kts/watirgrid">WatirGrid</a> to orchestrate a small flotilla of IE and FireFox based browsers based on <a href="http://watir.com">Watir</a></li>
<li><a href="http://jakarta.apache.org/jmeter/index.html">JMeter</a> to add more at the protocol level of performance testing</li>
<li><a href="http://www.hpl.hp.com/research/linux/httperf/">httperf</a> to do some basic benchmarking, similar to my front page</li>
<li><a href="https://github.com/90kts/numbrcrunchr">numbrcrunchr</a> to pull together system metrics and make for easier analysis</li>
<li>A <a href="https://www.zettagrid.com/index.php/about-news/157-altentee-benchmark-comparisons-between-australian-cloud-providers">variety of Australian cloud providers </a>and of course <a href="http://aws.amazon.com/ec2/pricing/">Amazon EC2</a> to host the test environment</li>
</ol>
<p>It has been a great year. I&#8217;m not entirely free of the commercial chains just yet and am still needing the LR type work to prop up this approach, but I hope 2011 brings about some fresh thinking in performance and test automation with hopefully me somewhere amidst that space.</p>
]]></content:encoded>
			<wfw:commentRss>http://altentee.com/blogs/2010/a-year-in-review-and-a-consolidated-approach/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>In. Ter. View. Questions</title>
		<link>http://altentee.com/blogs/2009/in-ter-view-questions/</link>
		<comments>http://altentee.com/blogs/2009/in-ter-view-questions/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 07:07:30 +0000</pubDate>
		<dc:creator>Tim Koopmans</dc:creator>
				<category><![CDATA[Altentee]]></category>
		<category><![CDATA[management]]></category>

		<guid isPermaLink="false">http://90kts.com/blog/2009/in-ter-view-questions/</guid>
		<description><![CDATA[Well, as a performance test lead I&#8217;ve been getting more involved in the hiring process. To cut through the chaff, these are some of the questions I like to ask potential candidates. What&#8217;s your favourite questions? How might you answer these? I also like to follow up these questions with a half hour practical scripting [...]]]></description>
			<content:encoded><![CDATA[<p>Well, as a performance test lead I&#8217;ve been getting more involved in the hiring process.</p>
<p>To cut through the chaff, these are some of the questions I like to ask potential candidates. What&#8217;s your favourite questions? How might you answer these?</p>
<p>I also like to follow up these questions with a half hour practical scripting in VuGen (or your tool of choice). You can learn alot about someone by the way they write their scripts.</p>
<p>The point? Determine if the person has recently used the tool (you&#8217;d be surprised) and suss out general knowledge of performance testing&#8230;</p>
<p><span id="more-239"></span><br />
1. What are essential components of a performance test plan? Give an<br />
example of a non functional requirement.<br />
<em>Test objectives, scope, scripts, scenarios, management, schedule, data,<br />
environment, access, defect management, traceability requirements, roles &#038;<br />
responsibilities.</p>
<p>An example NFR might be response time and/or volumetric requirements. e.g.<br />
this application/transaction is targeting a 5 second response time, with 20<br />
concurrent users and peak 20 trans/min.<br />
</em></p>
<p>2. Describe steps you&#8217;d take to setup a script / load harness and confirm<br />
correct operation / configuration.<br />
<em>Document manual test case, dry run, record 1st time, record 2nd time to<br />
help diff dynamic data, parametize &#038; correlate, replay. Validate using<br />
server logs, database or other areas which demonstrate correct application<br />
functionality.<br />
</em></p>
<p>3. What OS level resource metrics would you use to detect bottlenecks?<br />
<em>CPU, memory, disk, network</em></p>
<p>4. Which OS commands would you typically use to collect those metrics<br />
(Solaris, AIX etc)?<br />
<em>sar, prstat, vmstat, iostat, netstat etc</em></p>
<p>5. What are 2 advantages of using Performance Center?<br />
<em>Provide a central repository of scripts and their results<br />
Simplify licensing and distribution of software<br />
</em></p>
<p>6. Describe differences between correlation and parameterization in a LR<br />
script.</p>
<p><em><strong>Correlation</strong><br />
To simplify or optimize your code<br />
For example, if you perform a series of dependent queries one after<br />
another, your code may become very long. To reduce the size of the code,<br />
you can nest the queries, but then you lose precision and the code becomes<br />
complex and difficult to understand. Correlating the statements enables you<br />
to link queries without nesting.</p>
<p>To generate dynamic data<br />
Many applications and Web sites identify a session by the current date and<br />
time. If you try to replay a script, it will fail because the current time<br />
is different than the recorded time. Correlating the data enables you to<br />
save the dynamic data and use it throughout the scenario run.</p>
<p>To accommodate unique data records<br />
Certain applications (for example databases) require the use of unique<br />
values. A value that was unique during recording is no longer unique for<br />
script execution. For example, suppose you record the process of opening a<br />
new bank account. Each new account is assigned a unique number which is<br />
unknown to the user. This account number is inserted into a table with a<br />
unique key constraint during recording. If you try to run the script as<br />
recorded, it tries to create an account with the recorded number, rather<br />
than a new unique number. An error will result because the account number<br />
already exists.</p>
<p><strong>Parameterization</strong><br />
When you record a business process, VuGen generates a script that contains<br />
the actual values used during recording. Suppose you want to perform the<br />
script&#8217;s actions (query, submit, and so forth) using different values from<br />
those recorded. To do this, you replace the recorded values with<br />
parameters. This is known as parameterizing the script.<br />
</em></p>
<p>7. What LR function would you use to save a parameter in a web-based<br />
script?<br />
<em>web_reg_save_param()<br />
</em></p>
<p>8. Name 3 types of performance test types or methodologies you might employ<br />
in a typical test effort?<br />
<em>Shakeout, Baseline, Load, Volume, Stress, Soak, Tuning<br />
</em></p>
<p>9. Which blogs/user groups/forums would you use to help solve VUGen<br />
scripting difficulties?<br />
<em>Google<br />
HP support site<br />
SQA forums<br />
Yahoo or Google user groups<br />
90kts <img src='http://altentee.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
</em></p>
<p>10. Broadly describe typical run-time settings you&#8217;d employ in your VUGen<br />
script.<br />
<em>Run logic set to number of iterations<br />
Pacing to control start of iterations<br />
Log settings appropriate for test type<br />
Think time as recorded or random percentage<br />
Browser emulation with/without cache<br />
</em></p>
<p>11. What other types of resource/application level metrics might you<br />
monitor? Which tools would you require?<br />
<em>Web Servers &#8211; availability, bytes tx/rx, workers, requests, memory, cpu<br />
Application Servers &#8211; JDBC pools, thread pools, servlet sessions,  EJBs,<br />
JVM (memory util, garbage collection)<br />
Database Servers &#8211; server, tablespace and database metrics, locks, buffer<br />
ppols, reads, writes, slow queries<br />
Messaging Servers &#8211; queue stats, message flows, node statistics<br />
Network Devices &#8211; packets/bytes rx/tx, errors, discards<br />
</em></p>
<p>12. How does SiteScope facilitate agentless monitoring of unix hosts?<br />
<em>Uses ssh sessions to log in and retrieve OS and app level metrics from<br />
command line.<br />
</em></p>
<p>13. Define the differences between response time, throughput and latency.<br />
<em>Response time is normally end to end time taken for a round trip response. Can include many sub-components of the system under test.Normally expressed in milliseconds or seconds.</p>
<p>Throughput is amount of work a process can do in a period of time. Normally<br />
expressed as transactions/second or similar.</p>
<p>Latency is a period of delay. Normally expressed in milliseconds or<br />
seconds.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://altentee.com/blogs/2009/in-ter-view-questions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Performance Management for Web 2.0 Solutions</title>
		<link>http://altentee.com/blogs/2009/performance_management_for_web_2_solutions/</link>
		<comments>http://altentee.com/blogs/2009/performance_management_for_web_2_solutions/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 05:50:26 +0000</pubDate>
		<dc:creator>Tim Koopmans</dc:creator>
				<category><![CDATA[Altentee]]></category>
		<category><![CDATA[management]]></category>

		<guid isPermaLink="false">http://test.altentee.com/38/</guid>
		<description><![CDATA[Executive Summary Before we proceed to discuss Performance Management in the world of Web 2.0 solutions, let us briefly examine where the core performance management disciplines evolved from. By doing so we can see that the fundamentals of Performance Management have not changed but in fact are required more than ever. I do not want [...]]]></description>
			<content:encoded><![CDATA[<h2><strong>Executive Summary</strong></h2>
<p>Before we proceed to discuss Performance Management in the world of Web 2.0 solutions, let us briefly examine where the core performance management disciplines evolved from. By doing so we can see that the fundamentals of Performance Management have not changed but in fact are required more than ever.</p>
<p><span id="more-38"></span></p>
<p>I do not want to highlight my age, neither promote myself as a mainframe bigot. But the mainframe did establish many mature software engineering disciplines, such as release, change, configuration, environment, problem and performance management. All of these disciplines fed into mature processes such as the Unified Process, which are still used today for the development of new software platforms.</p>
<p>When talking specifically about performance management, in the days when mainframes were the sole core delivery source of customer data, there was a strong performance management discipline. This discipline provided assurance and risk mitigation to the owners of these systems that the customers would not receive any negative experiences when interacting with these systems such as bad response times or system outages. Using the IBM MVS mainframe as an example, there were such tools as OMEGAMON for monitoring queues, cpu, disk and memory, RMFMON and SMF for performance sampling response times, SAS for statistical analysis and TPNS for load simulation. Proactive processes were in place to ensure that the IT departments were aware of any potential performance impacts prior to the customer finding them. Large financial institutions were the major supporters of performance management in this area as they understood system outages could lead to large monetary losses. Today, mainframes continue to be supported by a strict performance management regime.</p>
<p>As the move was made into developing client/server solutions, and progressed to web based solutions, these disciplines, specifically Performance Management, seemed to become temporarily forgotten or maybe just received less priority. This was short lived after a large number of massive failures and subsequent monetary losses were encountered as a result of web site failures caused by halts and excessive response times. Soon the once forgotten discipline of performance testing was once again included as a core phase of the software development life cycle. Once again test managers, project managers and even senior executive management members were talking about performance testing activities, results, success or failure of KPIs or Service Level Agreements (SLAs) prior to deployment. However, a lot of companies, primarily due to costs involved in performance testing activities, still omitted these activities and suffered the consequences. Although, even those companies who performance tested their applications prior to deployment suffered later as they neglected to encompass the whole performance management package, that used to protect the mainframe from system failures. For performance testing, although a major contributor to the Performance Management solution, is only part of an overall holistic performance management solution.<br />
As there is more hype and talk about Web 2.0 solutions, there is even more necessity for IT departments to deliver a complete Performance Management package. The end user experience is now more complicated to gauge using the old Load Testing 1.0 philosophy of neglecting those components past an external firewall, even the client performance itself had been previously ignored. We will explain in the next section why performance risk areas in Web 2.0 solutions are more prevalent .</p>
<p>This whitepaper will explain to the reader what is required to deliver a complete holistic approach when mitigating performance risks. It will explain what should be incorporated in a Performance Management package and also highlight why companies should ensure they encompass the complete set of Performance Management disciplines, rather than a small subset of them when preparing to embark on any new software development project.</p>
<h3>Web 2.0 Performance Risk Areas</h3>
<p>Before we get into the detail of what performance management is lets just stop here for a minute and discuss at a high level some performance risk areas within Web 2.0 solutions.</p>
<p>Please refer to the many whitepapers on the internet that will provide a more in depth explanation of what is meant by Web 2.0 solutions. For our purpose here, we will provide a brief description of what is meant by it, highlighting why this has more performance impacts if not properly managed.</p>
<h4>Client Rendering</h4>
<p>Web 2.0 solutions now deliver more functionally rich capabilities to the web browser. Technologies such as Flex from Macromedia, as an example, take the processing responsibilities away from the server and place them on the client.</p>
<p>Ajax controls and more client powered java scripts, empower the client browser, but now place more importance on the performance measurement of what we call client rendering. Client rendering is the process that the web browser takes to present the complete refreshed response to the end user.</p>
<p>Client rendering could just be the populating of an Ajax drop down list control, or it could be the time take to completely draw that all important Sales graph. Performance testing for Web 1.0 solutions has normally focussed on server performance, although this is still important we need to ensure that we now measure response times on the client as well. A complete end to end user response needs to be measured when the applications servers are not under load, and then when they are under load, to determine whether or not the the response time is acceptable.</p>
<h4>Web Services</h4>
<p>More Web 2.0 solutions are utilising the flexibility of using external web services as defined by the World Wide Web Consortium (W3C). There are many obvious benefits in utilising external web services, specifically in saving development time and costs. However, your application now has a dependency on performance of an external site. How do you ensure that this performance is adequate?</p>
<h4>Geographical Locations</h4>
<p>As we know, the benefit of the internet is that users of web site applications can be geographically dispersed. However the downside of geographically dispersed users is determining how to measure the response times of a new web site under test, when so many variables are involved.</p>
<p>With the development of Web 2.0 solutions, the client becomes more functionally rich, and more resource intensive. The complete end to end user experience for every user, no matter their geographical location, now has to be managed by a performance management solution.</p>
<h4>RSS Feeds</h4>
<p>There has been a rapid increase in the amount of web sites using RSS (Rich Site Summary) feeds. Basically the RSS feed provides a format to deliver regularly changing content to your website such as news, weather and stock prices as an example.</p>
<p>Again, we now have external dependencies on performance. Also the feeds into the web site will have an unknown performance impact to the other processes running on the clients computer, which will impact the users experience with the web site.<br />
These are some areas with Web 2.0 solutions that magnify the necessity to performance manage your web site. Together with the existing issues surrounding web 1.0 solutions, there is now more than ever a need to encompass the performance management regime.</p>
<h3>What do we mean by Performance Management?</h3>
<p>Now we have highlighted some of the issues associated with the deployment of new web site applications specifically Web 2.0 solutions, lets now elaborate on performance management.</p>
<p>When it comes to discussing performance most IT practitioners are now aware of what we refer to as performance testing, although a lot still do not understand how this is done in a properly focussed approach, but that is another white paper in itself. However, as we mentioned previously, performance testing is one aspect of performance management.</p>
<p>A complete performance management solution, will performance test in a controlled approach, in an appropriate environment, before live deployment. It will then continue to performance regression test subsequent releases, to ensure no performance degradation has occurred between releases. A capacity planning discipline should be adopted for the live site, where statistics and models are reviewed on a regular basis. The user end experience should be proactively monitored using an appropriate dashboard. Lastly all server metrics should be monitored and fed into a viewable dashboard realtime.</p>
<p>That in a nut shell is what we mean when we talk about Performance Management. Now lets discuss some of these aspects in further detail.</p>
<h3>Performance Testing</h3>
<p>This deserves a white paper of its own. So without spending too much time on it here, check our website www.altentee.com for more on the different types of performance test types.</p>
<p>The most important ingredients into successfully performance testing is focussing on the high volume and business critical functionality of the web site under test. With the emphasis on end user response times, it is now necessary to not only performance test the appropriate servers, but to include the client rendering response time measurement in overall end to end response time measurement.</p>
<p>By utilising some open source alternatives to reduce costs, such as JMeter or OpenSTA, web traffic can be successfully emulated in varying loads on the web, file, application and database servers, by mimicking the browser incoming traffic. Whilst the servers are being loaded in this manner, geographically dispersed real browsers can emulate real user response times from varying locations. This can be achieved via cloud computing and such tools as BrowserMob, which altentee has an alliance with.<br />
For the test environment and load generators, the Amazon Elastic Cloud Computing (EC2) facilities can be leveraged to again massively reduce the associated costs.</p>
<p>Throughout the various performance tests, server metrics and user end response times should be collected and examined. Refer to the monitoring section discussed further on.</p>
<p>To ensure no performance degradation is encountered between code releases or any other changes, rapid performance tests should be conducted prior to any changes being deployed to production. Technical impact assessments can be conducted to determine what specific performance tests should be executed.</p>
<p>Sometimes it is forgotten that only one bad line of code, or one incorrect value of a configuration file can cause serious application performance problems, and can be a pest to find in production.</p>
<h3>Capacity Planning</h3>
<p>A proper capacity planning discipline should be adopted for any new website. This can be as simple as a projection model which can determine when links need upgrading, or new hard disk or memory will be required.</p>
<p>A projection model can be prepared based on ballpark projection figures, either sourced from historical data or from business projections. By regularly reviewing real statistics from the usage of the site by examining web logs and other sources, the model can be updated to ensure more realistic projections. Figures from the server monitoring solution, discussed later can feed into the model as well.</p>
<p>Capacity Planning is a logical exercise that when done correctly can save your web site a lot of downtime.</p>
<h3>Proactive Performance Monitoring</h3>
<p>The performance monitoring solution is the glue that holds the performance management solution together. It is vital that you proactively monitor the performance of both your production and test environments.</p>
<p>By proactively monitoring performance we mean consistently collecting server metrics and user end response times from all your environments, and reviewing these on a regular basis. Better still, setting up various thresholds and automatically receiving alerts once these thresholds are breached. This approach is in contrast to the passive monitoring approach, where performance metrics are only examined once a problem has been reported by an end user.</p>
<p>The key to performance monitoring is not only monitoring all the vital server metrics, but also monitoring the critical business processes from a users perspective. These can be done by automating these processes and running them on a regular basis.</p>
<h3>Conclusion</h3>
<p>By adopting the three key performance management areas of performance testing, capacity planning and proactive performance monitoring, you will be well on the way to guarding your website from costly outages. You can also safely support and encourage any business campaign or promotion that leads to increased traffic to your web site.</p>
<address>About the author</address>
<p>Nick Singh is an Australian based Performance Testing practitioner. He is a co-founder of <strong>Altentee</strong>, a company that prides itself on focused and cost effective performance management solutions. Nick has 28 years experience in IT of which 23 years have been dedicated to his passion for performance testing.</p>
<address>About Altentee</address>
<p><strong>Altentee</strong> is a company based in Melbourne, Australia that specialises in performance management solutions. It prides itself on expertise in performance management and has expert knowledge in all toolsets both commercial and open source. This allows the company not only to provide quality performance management solutions, but cost effective solutions as well.<br />
For further information on how Altentee can help your company, please contact Altentee on<br />
Phone 03 9005 NTEE<br />
Or +61 3 9005 6833</p>
<p>Suite 71, Building 2, 574 Plummer St<br />
Port Melbourne VIC 3207<br />
Australia</p>
]]></content:encoded>
			<wfw:commentRss>http://altentee.com/blogs/2009/performance_management_for_web_2_solutions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Throughput vs. Latency</title>
		<link>http://altentee.com/blogs/2008/throughput-vs-latency/</link>
		<comments>http://altentee.com/blogs/2008/throughput-vs-latency/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 07:03:31 +0000</pubDate>
		<dc:creator>Tim Koopmans</dc:creator>
				<category><![CDATA[Altentee]]></category>
		<category><![CDATA[management]]></category>

		<guid isPermaLink="false">http://90kts.com/blog/2008/throughput-vs-latency/</guid>
		<description><![CDATA[When/if I am asked to conduct interviews again for potential performance test analysts, I think I will include this question in my repertoire to suss out those in the know, and those just, um, pretending &#8230; Here is a great analogy of throughput and latency. I don&#8217;t pretend to understand queuing theory (just yet), but [...]]]></description>
			<content:encoded><![CDATA[<p>When/if I am asked to conduct interviews again for potential performance test analysts, I think I will include this question in my repertoire to suss out those in the know, and those just, um, pretending &#8230;</p>
<p>Here is a great <a href="http://www.forsythesunsolutions.com/node/114">analogy of throughput and latency</a>. I don&#8217;t pretend to understand <a href="http://en.wikipedia.org/wiki/Queueing_theory"><strong>queuing theory</strong></a> (just yet), but am working on it <img src='http://altentee.com/blogs/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://altentee.com/blogs/2008/throughput-vs-latency/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Job Interview Question #11 &#8211; Summing Numbers</title>
		<link>http://altentee.com/blogs/2008/job-interview-question-11-summing-numbers/</link>
		<comments>http://altentee.com/blogs/2008/job-interview-question-11-summing-numbers/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 06:07:49 +0000</pubDate>
		<dc:creator>Tim Koopmans</dc:creator>
				<category><![CDATA[Altentee]]></category>
		<category><![CDATA[management]]></category>

		<guid isPermaLink="false">http://90kts.com/blog/2008/job-interview-question-11-summing-numbers/</guid>
		<description><![CDATA[I&#8217;ve recently started following a blog which sets weekly programming challenges. I&#8217;m getting sick of Nintendo DS brain training and thought I&#8217;d try something more relevant to my line of work. Also, this gives me a good opportunity to learn more about my favoured scripting language at present, which is Ruby. So this week&#8217;s challenge [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently started following a <strong><a href="http://www.dev102.com/misc/a-programming-job-interview-challenge-11-summing-numbers/">blog</a></strong> which sets weekly programming challenges. I&#8217;m getting sick of Nintendo DS brain training and thought I&#8217;d try something more relevant to my line of work. Also, this gives me a good opportunity to learn more about my favoured scripting language at present, which is Ruby. So this week&#8217;s challenge states:</p>
<blockquote><p>Given a list of n integers and another integer called m, determine (true / false) if there exist 2 numbers in that list which sum up to m.<br />
Example: 2,6,4,9,1,12,7 and m=14 -> 2 and 12 sum up to 14, so the answer is true.<br />
Provide the best algorithm in both manners: performance and memory to solve this puzzle. Donâ€™t forget to mention the complexity of your solution!</p></blockquote>
<p>Read on for my thoughts in solving this challenge.<br />
<span id="more-177"></span></p>
<p>Looking at the problem, you obviously start with an array of numbers called n. If the sum of two numbers from the array is m, then you know that the array *may* contain two elements which are equal to m, such as n[a] + n[b] = m.</p>
<p>However you don&#8217;t know which two elements of the array might achieve this, nor do you know how many pairs in the array would satisfy the same conditions.</p>
<p>So my first thoughts were to iterate through each element of the array, then for each element, subtract that value from m, and iterate through remaining elements in the array looking for that value.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> test_nosort<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
      found <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>x.<span style="color:#9900CC;">to_i</span> == b<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The problem with this approach, is that for a starting array size of n, you have to check all possible combinations or n*n-1. Say for an array of size 100 elements, you have to check each value in the array 100-1 times (excluding current value), which means 100*99 = 9,900 possible combinations!</p>
<p>In the example provided above, I used the #each iterator for the array believing this to be the fastest operation when iterating through an array in Ruby.</p>
<p>This approach is suitable for small sized arrays (< 100 elements), but quickly becomes unusable when scaled up. In hindsight, this approach seems very similar to a <a href="http://en.wikipedia.org/wiki/Bubble_sort"><strong>bubble sort</strong></a> algorithm, where the list to be sorted has two items compared at a time and swapping them if they&#8217;re in the wrong order; iterating as many times until no more swaps are needed. It has a worst-case complexity described as n^2 where n is the number of items being sorted.</p>
<p>In the next approach, I decided to give regex a try ( I love regex <img src='http://altentee.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ), where in this case I still use an #each iterator to loop through the array, but use a regex grep comparison against the original array. The following code demonstrates:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> test_regex<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    found <span style="color:#006600; font-weight:bold;">+</span>= n.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">length</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This approach performed better than the first approach. I also experimented with some other array methods such as #select, #detect and #partition all of which gave varying but slow results.</p>
<p>After a much needed coffee and re-think, I decided to build a hash, where each key was based on the original values in my array, with the key-value equaling a count of all occurrences for that key&#8230; Then I could just use the array #each iterator and jump straight to the number of values that match that key. The following code demonstrates:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> test_hash<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  hash = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    hash.<span style="color:#9900CC;">has_key</span>? a ? hash<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span> = hash<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span> : hash<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    found <span style="color:#006600; font-weight:bold;">+</span>= hash<span style="color:#006600; font-weight:bold;">&#91;</span>b<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">if</span> hash.<span style="color:#9900CC;">has_key</span>? b
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This approach was a marked success over previous approaches and it scales well for large arrays of numbers. You can see the differences in time (using the benchmark gem) for each approach when using an array size of 100 elements as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">      user     <span style="color:#CC0066; font-weight:bold;">system</span>      total        real
test_nosort results :
  <span style="color:#006666;">0.530000</span>   <span style="color:#006666;">0.000000</span>   <span style="color:#006666;">0.530000</span> <span style="color:#006600; font-weight:bold;">&#40;</span>  <span style="color:#006666;">0.534364</span><span style="color:#006600; font-weight:bold;">&#41;</span>
test_sort results   :
  <span style="color:#006666;">0.530000</span>   <span style="color:#006666;">0.000000</span>   <span style="color:#006666;">0.530000</span> <span style="color:#006600; font-weight:bold;">&#40;</span>  <span style="color:#006666;">0.534992</span><span style="color:#006600; font-weight:bold;">&#41;</span>
test_regex results  :
  <span style="color:#006666;">0.360000</span>   <span style="color:#006666;">0.000000</span>   <span style="color:#006666;">0.360000</span> <span style="color:#006600; font-weight:bold;">&#40;</span>  <span style="color:#006666;">0.357073</span><span style="color:#006600; font-weight:bold;">&#41;</span>
test_detect results :
  <span style="color:#006666;">6.390000</span>   <span style="color:#006666;">0.040000</span>   <span style="color:#006666;">6.430000</span> <span style="color:#006600; font-weight:bold;">&#40;</span>  <span style="color:#006666;">6.480277</span><span style="color:#006600; font-weight:bold;">&#41;</span>
test_select results :
  <span style="color:#006666;">0.400000</span>   <span style="color:#006666;">0.000000</span>   <span style="color:#006666;">0.400000</span> <span style="color:#006600; font-weight:bold;">&#40;</span>  <span style="color:#006666;">0.403032</span><span style="color:#006600; font-weight:bold;">&#41;</span>
test_partn results  :
  <span style="color:#006666;">0.600000</span>   <span style="color:#006666;">0.010000</span>   <span style="color:#006666;">0.610000</span> <span style="color:#006600; font-weight:bold;">&#40;</span>  <span style="color:#006666;">0.623047</span><span style="color:#006600; font-weight:bold;">&#41;</span>
test_hash results   :
  <span style="color:#006666;">0.020000</span>   <span style="color:#006666;">0.000000</span>   <span style="color:#006666;">0.020000</span> <span style="color:#006600; font-weight:bold;">&#40;</span>  <span style="color:#006666;">0.021390</span><span style="color:#006600; font-weight:bold;">&#41;</span>
test_binary results :
  <span style="color:#006666;">0.270000</span>   <span style="color:#006666;">0.000000</span>   <span style="color:#006666;">0.270000</span> <span style="color:#006600; font-weight:bold;">&#40;</span>  <span style="color:#006666;">0.273587</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Even with array sizes of 10,000+ elements the benchmark was in the order of seconds.</p>
<p>However with an even larger array size of say 100,000+ elements, the memory usage of this hash search would be significant. I also tried a <strong><a href="http://en.wikibooks.org/wiki/Algorithm_implementation/Search/Binary_search">binary search</a></strong> but found this had worse performance than a hash. From reading wikipedia though it would appear that a <a href="http://en.wikipedia.org/wiki/Binary_search_tree"><strong>binary tree search</strong></a> might be the best bet in terms of memory usage for large arrays.</p>
<p><strong>The Answer?</strong></p>
<p>For now I am satisfied that a <strong>hash key search</strong> is the least complex way to deal with this particular challenge, with the only caveat being a ceiling limit on the amount of elements that are in your initial array to begin with e.g. 1 &#8211; 10,000 elements. For larger arrays, <strong>binary tree algorithms</strong> might offer better memory performance but would be more complex to implement in a scripting language like Ruby.</p>
<p>The code I used to test this is attached below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'benchmark'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># m = 14</span>
m = <span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># n = [2,6,4,9,1,12,7]</span>
n = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> e = <span style="color:#CC0066; font-weight:bold;">rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#0066ff; font-weight:bold;">@debug</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_nosort<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
      found <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>x.<span style="color:#9900CC;">to_i</span> == b<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_sort<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">sort</span>!
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
     b = m <span style="color:#006600; font-weight:bold;">-</span> a
    n.<span style="color:#9900CC;">reverse_each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
      found <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>x.<span style="color:#9900CC;">to_i</span> == b<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_regex<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    found <span style="color:#006600; font-weight:bold;">+</span>= n.<span style="color:#9900CC;">grep</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">length</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_detect<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    found <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> n.<span style="color:#9900CC;">detect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">/</span>\b<span style="color:#008000; font-style:italic;">#{b}\b/ =~ x.to_s }</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_select<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    found_array = n.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x == b <span style="color:#006600; font-weight:bold;">&#125;</span>
    found <span style="color:#006600; font-weight:bold;">+</span>= found_array.<span style="color:#9900CC;">length</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_partn<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    found_array = n.<span style="color:#9900CC;">partition</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x == b <span style="color:#006600; font-weight:bold;">&#125;</span>
    found <span style="color:#006600; font-weight:bold;">+</span>= found_array.<span style="color:#9900CC;">length</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
   <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_hash<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  hash = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    hash.<span style="color:#9900CC;">has_key</span>? a ? hash<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span> = hash<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span> : hash<span style="color:#006600; font-weight:bold;">&#91;</span>a<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    found <span style="color:#006600; font-weight:bold;">+</span>= hash<span style="color:#006600; font-weight:bold;">&#91;</span>b<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">if</span> hash.<span style="color:#9900CC;">has_key</span>? b
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_binary<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>
  found=<span style="color:#006666;">0</span>
  n.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
    b = m <span style="color:#006600; font-weight:bold;">-</span> a
    found <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> binary_search<span style="color:#006600; font-weight:bold;">&#40;</span>n, b<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> found <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@debug</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> binary_search<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">array</span>, val, left = <span style="color:#006666;">0</span>, right = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">array</span>.<span style="color:#9900CC;">sort</span>!
  right = <span style="color:#CC0066; font-weight:bold;">array</span>.<span style="color:#9900CC;">size</span> <span style="color:#9966CC; font-weight:bold;">unless</span> right;
  mid = <span style="color:#006600; font-weight:bold;">&#40;</span>left <span style="color:#006600; font-weight:bold;">+</span> right<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">2</span>;
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">if</span> left <span style="color:#006600; font-weight:bold;">&gt;</span> right
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">if</span> val == <span style="color:#CC0066; font-weight:bold;">array</span><span style="color:#006600; font-weight:bold;">&#91;</span>mid<span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> mid
  <span style="color:#9966CC; font-weight:bold;">elsif</span> val <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC0066; font-weight:bold;">array</span><span style="color:#006600; font-weight:bold;">&#91;</span>mid<span style="color:#006600; font-weight:bold;">&#93;</span>
    binary_search<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">array</span>, val, mid <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>, right<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    binary_search<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">array</span>, val, left, mid <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bm</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>timer<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#0066ff; font-weight:bold;">@debug</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
  timer.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test_nosort results :<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> 100.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test_nosort<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">&#125;</span>
  timer.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test_sort results   :<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> 100.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test_sort<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">&#125;</span>
  timer.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test_regex results  :<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> 100.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test_regex<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">&#125;</span>
  timer.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test_detect results :<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> 100.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test_detect<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">&#125;</span>
  timer.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test_select results :<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> 100.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test_select<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">&#125;</span>
  timer.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test_partn results  :<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> 100.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test_partn<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">&#125;</span>
  timer.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test_hash results   :<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> 100.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test_hash<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">&#125;</span>
  timer.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;test_binary results :<span style="color:#000099;">\n</span>&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> 100.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> test_binary<span style="color:#006600; font-weight:bold;">&#40;</span>m,n<span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># &gt;&gt;       user     system      total        real</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt; test_nosort results :</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt;   0.530000   0.000000   0.530000 (  0.534364)</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt; test_sort results   :</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt;   0.530000   0.000000   0.530000 (  0.534992)</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt; test_regex results  :</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt;   0.360000   0.000000   0.360000 (  0.357073)</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt; test_detect results :</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt;   6.390000   0.040000   6.430000 (  6.480277)</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt; test_select results :</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt;   0.400000   0.000000   0.400000 (  0.403032)</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt; test_partn results  :</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt;   0.600000   0.010000   0.610000 (  0.623047)</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt; test_hash results   :</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt;   0.020000   0.000000   0.020000 (  0.021390)</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt; test_binary results :</span>
<span style="color:#008000; font-style:italic;"># &gt;&gt;   0.270000   0.000000   0.270000 (  0.273587)</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://altentee.com/blogs/2008/job-interview-question-11-summing-numbers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

