简体   繁体   中英

How can I optimize my code so as to run faster

I am writing a code that can fetch some data Via API but it needs a Parameter called Item id which is stored in my database. Code is working fine I am getting what type of output I want but the issue is it is taking more time(it is not fast).

there are two functions

  1. Function Item_id to fetch Item_Id from the database which I have already in my SQL server.

  2. Main function which calls Item_id function to fetch Item_id's from the database. these Item Id's are used as a parameter in API query to fetch serial numbers for the serial numbers for that particular Item_id.


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using Newtonsoft.Json;


namespace ConsoleApp2
{
    class serial_connected_to_Database
    {
    /**************************************************
     * Properties to convert Json output to C# objects
     * ***********************************************/
       #region Properties
       public class SerialNumber
       {
           public string serialnumber_id { get; set; }
           public string serialnumber { get; set; }
           public string status { get; set; }
       }

       public class PageContext
       {
            public int page { get; set; }
            public int per_page { get; set; }
            public bool has_more_page { get; set; }
            public string sort_column { get; set; }
            public string sort_order { get; set; }
       }

       public class RootObject
       {
            public int code { get; set; }
            public string message { get; set; }
            public List<SerialNumber> serial_numbers { get; set; }
            public PageContext page_context { get; set; }
       }
       #endregion

    /***********************************************************
     * Main program to fecth serial Numbers based on the Item_id
     * specified in the API
     ***********************************************************/
       public static void Main()
       {
            string strResponse = string.Empty;  //to collect json Data
            DataTable dataTable = Item_Id();    //fetching item id from function Item_id
            foreach (DataRow dr in dataTable.Rows) 
            {
                Console.WriteLine(dr["col_item_id"]+"Serial numbers for particular Item id are given below");
                int k = 1; //variable to navigate to different pages
                while (1 > 0)
                {
                /****************************************************************
                 * Requesting API with item id as parameter
                 * *************************************************************/
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://books.zoho.com/api/v3/items/serialnumbers?per_page=200&item_id=" + dr["col_item_id"] + "&organization_id=***********&page=" + k);
                    request.Method = "GET";
                    request.Headers.Add("Authorization", "*************************");

                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new ApplicationException("Error code in response recieved: " + response.StatusCode.ToString());
                        }

                        using (Stream resStream = response.GetResponseStream())
                        {
                            if (resStream != null)
                            {
                                using (StreamReader streamReader = new StreamReader(resStream))
                                {
                                    strResponse = streamReader.ReadToEnd();
                                }
                            }
                       }
                   }
                   string Jsoncontent = strResponse;

                /***************************************************************
                 * Deserializing Json Data into C# objects
                 * ************************************************************/
                    RootObject root = JsonConvert.DeserializeObject<RootObject>(Jsoncontent);
                    if (root.serial_numbers.Count != 0)
                    {
                        for(int i = 0; i < root.serial_numbers.Count; i++)
                        {                           
Console.WriteLine(root.serial_numbers[i].serialnumber);
                        }
                    }
                    else
                    {
                        break;
                    }
                    k = k + 1;
                    Console.WriteLine("Page= " + k);
                }
            }
        }

    /******************************************************************************
     * Function to return Item id which is stored in Database 
     * ***************************************************************************/
        public static DataTable Item_Id()
        {
            SqlConnection cnn = new SqlConnection(@"Data Source=************;Initial Catalog=*************;User ID=*******************;Password=*************");
            cnn.Open();
            DataTable dataTable = new DataTable();

            string Query = "select col_item_id from tbl_Zoho_Items_data";

            SqlDataAdapter DA = new SqlDataAdapter(Query, cnn);
            DA.Fill(dataTable);
            return dataTable;
        }
    }
}

It takes a lot of time while executing please help me with some best way use write or any best practices.

A lot of the work is being done by external services - both HTTP requests and database queries. You cannot meaningfully optimise your code until you understand where the time is being spent - it may be that you cannot improve things because the bottlenecks are outside your application, but you cannot know that unless you profile your application, and/or mock out the external services. Get yourself a trial of a profiler and find out what your application is really doing - you'll learn a lot more that way.

I think it would be better to use DataReader other tan DataTable and to use Async methods for connections and for posting or you can get a list of items and you parallel.for method inside an async method I think this would more faster

I think like this one would be better and faster please test it and tell me

        /******************************************************************************
 * Function to return Item id which is stored in Database 
 * ***************************************************************************/
    public static async Task<IEnumerable<string>> Item_Id()
    {
        var items = new List<string>();

        using (var cnn = new SqlConnection(@"Data Source=************;Initial Catalog=*************;User ID=*******************;Password=*************"))
        {
            await cnn.OpenAsync().ConfigureAwait(false);
            var Query = "select col_item_id from tbl_Zoho_Items_data";

            using (var command = new SqlCommand(Query, cnn))
            {
                using (SqlDataReader reader = await command.ExecuteReaderAsync().ConfigureAwait(false))
                {
                    while (await reader.ReadAsync().ConfigureAwait(false)) items.Add(reader["col_item_id"].ToString());
                }
            }

            return items;
        }
    }

    /***********************************************************
 * Main program to fecth serial Numbers based on the Item_id
 * specified in the API
 ***********************************************************/
    public static async Task Main()
    {
        string strResponse = string.Empty; //to collect json Data
        IEnumerable<string> items = await Item_Id().ConfigureAwait(false); //fetching item id from function Item_id

        Parallel.ForEach(items, async item =>
        {
            Console.WriteLine(item + "Serial numbers for particular Item id are given below");

            var k = 1; //variable to navigate to different pages
            while (1 > 0)
            {
                /****************************************************************
                 * Requesting API with item id as parameter
                 * *************************************************************/
                var request = (HttpWebRequest) WebRequest.Create(@"https://books.zoho.com/api/v3/items/serialnumbers?per_page=200&item_id=" + item + "&organization_id=***********&page=" + k);
                request.Method = "GET";
                request.Headers.Add("Authorization", "*************************");

                using (var response = (HttpWebResponse) await request.GetResponseAsync().ConfigureAwait(false))
                {
                    if (response.StatusCode != HttpStatusCode.OK) throw new ApplicationException("Error code in response recieved: " + response.StatusCode.ToString());

                    using (Stream resStream = response.GetResponseStream())
                    {
                        if (resStream != null)
                            using (var streamReader = new StreamReader(resStream))
                            {
                                strResponse = await streamReader.ReadToEndAsync().ConfigureAwait(false);
                            }
                    }
                }

                string Jsoncontent = strResponse;

                /***************************************************************
                 * Deserializing Json Data into C# objects
                 * ************************************************************/
                var root = JsonConvert.DeserializeObject<RootObject>(Jsoncontent);
                if (root.serial_numbers.Count != 0)
                    foreach (SerialNumber serial in root.serial_numbers)
                        Console.WriteLine(serial.serialnumber);
                else
                    break;

                k++;
                Console.WriteLine("Page= " + k);
            }
        });
    }

you can try Applying cache technique in web api code. meaning, You will have to call db only if cache will not be available. you will get sample everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM